This problem you are having there is due to the fact of buffer keyboard not being clean, when you press enter gets a /n
stored in the keyboard buffer, to end this error just clear the buffer.
For this use the function setbuf
, it serves to assign a value to the buffer. In this case we will assign a null value, then before each fgets
you give you will add the following setbuf (stdin, NULL);
, because then you will be saying that you want to assign the value of NULL
, that is, zero to stdin
which is the keyboard buffer, such that its main
stay that way:
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, plus by good programming practices take that global variable that you are using, and incorporate it into the main
, because this way you have a code more beautiful and less susceptible to errors.
Plus it’s remarkable that when you type 3 APRIL 2001 you’re giving a buffer overflow, because you created a string, to store the date, of only 10 characters, remembering that the last one is reserved to the \0
soon you will only have 9 characters available, and this sentence, 03 APRIL 2001, has 20 counting on the \0
, then you would need a larger string, ie to solve the second error just increase the size of the string to a more suitable size, I believe 30 is more than enough, after the changes your code will be 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 particularly do not like the use of fgets
for this, I prefer to use the scanf
.
To be able to read everything \n
with the scanf
just do the following, scanf(" %[^\n]s", nome_da_string);
, so you’re adding a condition in it saying it’s for him to read up the \n
, that is, until the end of the line, also do not forget to put the space between the quotation marks (") and the percent (%), so you will not have buffer error, and will not have to clean the buffer, that way the code in the main
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 I’ve helped, anything just ask, and good luck in programming studies (^_^)/
This is buffer problem, try using setbuf(stdin,NULL); before fgets
– Gabriel A. S. Braga
So? printf (" n TYPE STUDENT NAME: "); setbuf(stdin,NULL); fgets(CAD_ALUNO.name, 50, stdin);
– Diego Roney
You set the date of birth with
char nasc[10];
but wrote03 DE MARÇO DE 2001
which are 20 characters, so it is incorrect and the line break was not stored because it did not fit. It is also worth remembering that size 20 are 19 letters because the\0
also has to be stored. I also remember that thefgets
guards the\n
I have already answered several questions with this problem, such as this.– Isac