4
Guys, good morning, I’m creating this simple code, just to read, and display, this vector information, like struct
.
Only that in executing this code, in the act of execution, I can not feed neither the date nor the affiliation, as it is explicit in the image below, as if there was no space between them.
How to solve?
Follows the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
struct bilhete{
int idade;
char nome[30];
char fi[30];
char data[30];
};
int main() {
struct bilhete identidade[5];
int cont;
for (cont = 0 ; cont <=5 ; cont++){
fflush(stdin);
printf("Informe o nome: \n");
gets(identidade[cont].nome);
printf("Informe a idade: \n");
scanf("%d",&identidade[cont].idade);
printf("Informe a filiacao: \n");
gets(identidade[cont].fi);
printf("Informe a data: \n");
gets(identidade[cont].data);
}
// EXIBIR
for (cont =0 ; cont <=5 ; cont++){
printf("Nome: %s",identidade[cont].nome);
printf("Idade: %d",identidade[cont].idade);
printf("Filiacao: %s",identidade[cont].fi);
printf("Data: %s",identidade[cont].data);
}
system("pause");
return 0;
}
Read this: http://answall.com/q/42981/101 and this: http://answall.com/q/111697/101
– Maniero
Quoting my own answer in another question: Never, ever, ever use the function
gets
. This function is hideous, and is universally and unanimously hated among C programmers. The reason is that it is simply impossible to use this function safely. Every time it’s used, your program already wins a giant hole by which it can catastrophically fail just as your program is failing. There is no correct way to use this function, all forms of use are incorrect.– Victor Stafusa
As to the
fflush(stdin)
, see that my other answer.– Victor Stafusa