Error with C Strings

Asked

Viewed 206 times

1

 A frase DIGITE A RUA só deveria aparecer logo apos ser inserido o que pede o DIGITE UM NUMERO PARA CONTATO

The phrase "TYPE THE STREET" should only appear immediately after being inserted what asks the "ENTER A NUMBER FOR CONTACT". 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;
}
  • This is buffer problem, try using setbuf(stdin,NULL); before fgets

  • So? printf (" n TYPE STUDENT NAME: "); setbuf(stdin,NULL); fgets(CAD_ALUNO.name, 50, stdin);

  • 1

    You set the date of birth with char nasc[10]; but wrote 03 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 the fgets guards the \n I have already answered several questions with this problem, such as this.

1 answer

1


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 (^_^)/

  • All right Gabriel, when picking up texts, for example name: ANGELA RIBEIRO SOUSA, so far everything Ok. But on DATE OF BIRTH: if I type : 03 APRIL 2001 or 03/04/2001 back the image error to top. It would be a problem with numbers?

  • I’ll edit here, adding the error q is giving the more, calm and a little

  • editing was done, hope to have helped, anything just send the question okay

  • Perfect! Helped a lot! All right!

  • fgets is preferred to scanf because it avoids buffer overflow which is something that does not happen in the question code, because the fgets never stores more than the specified size. And it is something that will happen in yours if I put a string larger than any of the defined sizes. However, it must have additional treatment for the \n which is kept when it is kept.

Browser other questions tagged

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