Does anyone know why you made a mistake?

Asked

Viewed 85 times

0

when I try to enter any character value the Skip program in the option and I don’t even get a chance to write anyone knows why?

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>

    int i=0;
    struct pessoas {
    char nomealuno[60];
    int numeromeca;
    char rua[90];
    char curso[200];
    int dia;
    int mes;
    int ano;
    int nporta;
    int codigopostal1;
    int codigopostal2;
    int num;
    };

     .....

    case 1:
    {  
    printf("Nome    : "); 
    fgets(aluno[i].nomealuno, sizeof(aluno[i].nomealuno), stdin);

    printf("Numero mecanografico  : "); 
    scanf("%d",&aluno[i].numeromeca);

    printf("Data de nascimento    :\n "); 
    printf("dia.:");
    scanf("%d",&aluno[i].dia);  

    printf("mes.:");
    scanf("%d",&aluno[i].mes); 

    printf("ano.:");
    scanf("%d",&aluno[i].ano);

    printf("Rua   : "); 
    fgets(aluno[i].rua, sizeof(aluno[i].rua), stdin);

    printf("N da porta   :"); 
    scanf("%d",&aluno[i].nporta);

    printf("codigo postal:"); 
    scanf("%d",&aluno[i].codigopostal1); 
    printf("-");                                         
    scanf("%d",&aluno[i].codigopostal2);

    printf("Em que curso pertence o aluno?"); 
    fgets(aluno[i].curso, sizeof(aluno[i].curso), stdin);
    }

    .....

erro no programa

  • 1

    Do not post code image, post the code itself. It is impossible to have a complete view of what you wrote here on my device

  • ok I’ll edit

  • these are the necessary excerpts but if you want I can put the full code

  • I think it’s just a problem in the read buffer. How do you read the option?

  • read as machine or person? xD

  • Programmatically read. scanf? fgets and then compile the die in the hand?

  • Mostrar_aluno () { printf("Nome.: [%d] %s n",aluno[i]. nomeluno); printf("Numero mecanografico.: %8d n", aluno[i]. numeromeca); printf("Date of Birth.:%2d / %2d / %4d n",student[i]. day, student[i]. month, student[i]. year); printf("Street .: [%d] %s n",student[i]. street); printf("N of the door .: Nº %3d n",pupil[i]. nporta); printf("postal code .: %4d-%3d n",pupil[i]. codigopostal1,pupil[i]. codigopostal2); printf("Course .: [%d] %n",student[i]. course); }

  • yes it is basically that way

  • I asked you between two options and you said "yes"?

  • then I did not understand what refers to sorry , can explain otherwise?

  • You have a variable that is used in switch. This variable is informed by the user. How the program reads this information the user provides?

  • the information is all given in the previous case by the user the program at the beginning has not given any

Show 7 more comments

1 answer

1

The problem is that the scanf read only the data that was indicated and not the line break typed, which will be just what the fgets will read next.

A robust solution is to read the integer also with fgets using a buffer temporary and sscanf:

char buff[20];
int numero;
fgets(buff, sizeof(buff), stdin); //lê como string
sscanf(buff, "%d", &numero); //interpreta o inteiro na string lida

If you want to simplify you can even make a function to encapsulate this logic whenever you need to read only one number:

char buff[20];

int lerInt(){
    int numero;
    fgets(buff, sizeof(buff), stdin);
    sscanf(buff, "%d", &numero);
    return numero;
}

Applying this logic your code would look like this:

printf("Nome    : ");
fgets(aluno[i].nomealuno, sizeof(aluno[i].nomealuno), stdin);

printf("Numero mecanografico  : ");
aluno[i].numeromeca = lerInt();

printf("Data de nascimento    :\n ");
printf("dia.:");
aluno[i].dia = lerInt();

printf("mes.:");
aluno[i].mes = lerInt();

printf("ano.:");
aluno[i].ano = lerInt();

printf("Rua   : ");
fgets(aluno[i].rua, sizeof(aluno[i].rua), stdin);

printf("N da porta   :");
aluno[i].nporta = lerInt();

printf("codigo postal:");
aluno[i].codigopostal1 = lerInt();
printf("-");
aluno[i].codigopostal2 = lerInt();

printf("Em que curso pertence o aluno?");
fgets(aluno[i].curso, sizeof(aluno[i].curso), stdin);

See the code running on Ideone

Browser other questions tagged

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