Error with Strings in C

1

Thephrase" ENTER THE STREET " should only appear as soon as you enter the " ENTER A NUMBER TO CONTACT" link. What must have happened?

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


typedef struct aluno
{

char nome[50];
char nasc[10]; 
char tel[20];
char end[20]; 
char bairro[10]; 
char cidade[10];
char mae[20]; 
char pai[20];
} aluno;
      aluno CAD_ALUNO;

int main()
{

printf ("\n CADASTRO DE ALUNO:");

printf ("\n DIGITE O NOME DO ALUNO: ");
fgets(CAD_ALUNO.nome, 50, stdin);

printf (" DIGITE A DATA DE NASCIMENTO: ");
fgets(CAD_ALUNO.nasc, 10, stdin);

printf (" DIGITE UM NUMERO PARA CONTATO: ");
fgets(CAD_ALUNO.tel, 20, stdin);

printf (" DIGITE A RUA ");
fgets(CAD_ALUNO.end, 20, stdin);

printf (" DIGITE O BAIRRO ");
fgets(CAD_ALUNO.bairro, 10, stdin);

printf (" DIGITE A CIDADE ");
fgets(CAD_ALUNO.cidade, 10, stdin);

printf (" DIGITE O NOME DA MAE ");
fgets(CAD_ALUNO.mae, 20, stdin);

printf (" DIGITE O NOME DO PAI ");
fgets(CAD_ALUNO.pai, 20, stdin);

system ("pause");
return 0;
}
    
asked by anonymous 07.07.2018 / 15:11

1 answer

1

This problem you're having is due to the fact that # buffer of the keyboard is not clean, when you press enter it gets a /n stored in the keyboard buffer, to end this error simply clear the buffer.

To do this use the function setbuf , it serves to assign a value to the buffer. In the case we will assign a null value, then before each fgets you give you will add the following setbuf (stdin, NULL); , so you are saying that you want to assign the value of NULL , ie zero to stdin is the keyboard buffer, so that your main looks like this:

int main()
{
printf ("\n CADASTRO DE ALUNO:");

printf ("\n DIGITE O NOME DO ALUNO: "); 
setbuf (stdin, NULL);
fgets(CAD_ALUNO.nome, 50, stdin); 

printf (" DIGITE A DATA DE NASCIMENTO: "); 
setbuf (stdin, NULL);
fgets(CAD_ALUNO.nasc, 10, stdin); 

printf (" DIGITE UM NUMERO PARA 
CONTATO: "); 
setbuf (stdin, NULL);
fgets(CAD_ALUNO.tel, 20, stdin); 

printf (" DIGITE A RUA ");
setbuf (stdin, NULL);
fgets(CAD_ALUNO.end, 20, stdin); 

printf (" DIGITE O BAIRRO "); 
setbuf (stdin, NULL);
fgets(CAD_ALUNO.bairro, 10, stdin); 

printf (" DIGITE A CIDADE "); 
setbuf (stdin, NULL);
fgets(CAD_ALUNO.cidade, 10, stdin); 

printf (" DIGITE O NOME DA MAE "); 
setbuf (stdin, NULL);
fgets(CAD_ALUNO.mae, 20, stdin); 

printf (" DIGITE O NOME DO PAI "); 
setbuf (stdin, NULL);
fgets(CAD_ALUNO.pai, 20, stdin);

system("pause");
returne 0;
}

This is the first step, in addition to good programming practices, take that global variable you are using, and incorporate it into main , so you have a more beautiful and less error-prone code.

Furthermore it is notable that when you type 03 APRIL 2001 you are giving a buffer overflow because you have created a string to store the date of only 10 characters, remembering that the last one is reserved for fgets soon you will have only 9 characters available, and this sentence, APRIL 03, 2001, has 20 counting scanf , then you would need a larger string, ie to solve the second error just increase the size of the string to a more appropriate size, I believe that 30 is more than enough, after the changes your code will look like this:

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


typedef struct aluno
{

    char nome[50];
    char nasc[30];
    char tel[20];
    char end[20];
    char bairro[10];
    char cidade[10];
    char mae[20];
    char pai[20];
} aluno;

int main()
{
    aluno CAD_ALUNO;

    printf ("\n CADASTRO DE ALUNO:");

    printf ("\n DIGITE O NOME DO ALUNO: ");
    setbuf (stdin, NULL);
    fgets(CAD_ALUNO.nome, 50, stdin);

    printf (" DIGITE A DATA DE NASCIMENTO: ");
    setbuf (stdin, NULL);
    fgets(CAD_ALUNO.nasc, 30, stdin);

    printf (" DIGITE UM NUMERO PARA CONTATO: ");
    setbuf (stdin, NULL);
    fgets(CAD_ALUNO.tel, 20, stdin);

    printf (" DIGITE A RUA ");
    setbuf (stdin, NULL);
    fgets(CAD_ALUNO.end, 20, stdin);

    printf (" DIGITE O BAIRRO ");
    setbuf (stdin, NULL);
    fgets(CAD_ALUNO.bairro, 10, stdin);

    printf (" DIGITE A CIDADE ");
    setbuf (stdin, NULL);
    fgets(CAD_ALUNO.cidade, 10, stdin);

    printf (" DIGITE O NOME DA MAE ");
    setbuf (stdin, NULL);
    fgets(CAD_ALUNO.mae, 20, stdin);

    printf (" DIGITE O NOME DO PAI ");
    setbuf (stdin, NULL);
    fgets(CAD_ALUNO.pai, 20, stdin);

    system ("pause");
    return 0;
}

In addition there are also other ways to read a string with spaces, I do not particularly like using \n for this, I'd rather use scanf .

To be able to read everything up to scanf(" %[^\n]s", nome_da_string); with \n just do the following, main , so you're adding a condition in it saying that it is for it to read %code% , end of the line, also be sure to put the space between the quotation marks (") and the percent (%), so that you will not have a buffer error, and will not have to clear the buffer, so the code in %code% would look like this:

int main()
{
    aluno CAD_ALUNO;

    printf ("\n CADASTRO DE ALUNO:");

    printf ("\n DIGITE O NOME DO ALUNO: ");
    scanf(" %[^\n]s", CAD_ALUNO.nome);

    printf (" DIGITE A DATA DE NASCIMENTO: ");
    scanf(" %[^\n]s", CAD_ALUNO.nasc);

    printf (" DIGITE UM NUMERO PARA CONTATO: ");
    scanf(" %[^\n]s", CAD_ALUNO.tel);

    printf (" DIGITE A RUA ");
    scanf(" %[^\n]s", CAD_ALUNO.end);

    printf (" DIGITE O BAIRRO ");
    scanf(" %[^\n]s", CAD_ALUNO.bairro);

    printf (" DIGITE A CIDADE ");
    scanf(" %[^\n]s", CAD_ALUNO.cidade);

    printf (" DIGITE O NOME DA MAE ");
    scanf(" %[^\n]s", CAD_ALUNO.mae);

    printf (" DIGITE O NOME DO PAI ");
    scanf(" %[^\n]s", CAD_ALUNO.pai);

    system ("pause");
    return 0;
}

I hope to have helped, just ask, and good luck in programming studies (^ _ ^) /

    
07.07.2018 / 15:44