Help in struct C, failing to feed

4

Good morning, I'm creating this simple code, just to read, and display, this information of a vector, type struct .

But when executing this code, at the time of execution, I can not feed the date or the membership, as it is explicit in the image below, as if there was no space between them.

How to solve?

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

    
asked by anonymous 10.07.2016 / 17:40

1 answer

2

First of all, in% w / o%,% w / o% should not be% w / w to 5 but% w / o w < because every vector in C goes from 0 to N-1 .

The methods have different types of reading, for works equal to cont The difference is that in <= you can treat how you want to read the information. To correct this error, use < and pass the following format to read: gets .

This statement does not separate the value types, it simply reads what is in the terminal. So, if you put scanf('%[\^n]',...) at the beginning, it causes the information to read after the line break (there is no need to use scanf , you have to use scanf ).

"\n%30[^\n]" will read the next 30 characters until the line break. As a line breaker it is also a character, if you do not use \n at the start, the reader identifies a line break and understands as if you had already read the statement, as 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);
}
    
11.07.2016 / 07:13