Help in struct C, unable to feed

Asked

Viewed 124 times

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;
}

a busy cat

  • Read this: http://answall.com/q/42981/101 and this: http://answall.com/q/111697/101

  • 1

    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.

  • As to the fflush(stdin), see that my other answer.

1 answer

2

First, in his for, the cont shouldn’t be <= to 5 and yes < that 5. because every vector in C goes from 0 to N-1.

The methods have different types of reading, the gets works the same as scanf('%[\^n]',...) the difference is that in the scanf you can handle how you want to read the information. To correct this error, use the scanf and pass the following format to read: "\n%30[^\n]".

This instruction does not separate the types of values, it simply reads what is in the terminal. Then you put the \n at first, causes the reading of the information to occur after the line break (no need to use in the %d, %s, %f, %i, %lli, ...; need to use in %[^\n], %c).

The %30[^\n] will read the next 30 characters up to the line break. As line break is also character, if you do not use the \n at first, the reader identifies a line break and understands as if he had already read the instruction, because this system skips the reading.

for (cont = 0 ; cont < 5 ; cont++){
    printf("Informe o nome: ");
    scanf("\n%30[^\n]",identidade[cont].nome);

    printf("Informe a idade: ");
    scanf("%d",&identidade[cont].idade);

    printf("Informe a filiacao: ");
    scanf("\n%30[^\n]",identidade[cont].fi);

    printf("Informe a data: ");
    scanf("\n%30[^\n]",identidade[cont].data);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.