C - Error reading a character using scanf(): scanf() command is "skipped"

Asked

Viewed 117 times

1

I would like to know why my code does not accept reading characters, simply skip that step and perform the next.

float altura, pesoIdeal = 0;
char sexo;

printf("Informe a sua altura : ");
scanf("%f",&altura);

printf("Informe o seu sexo : ");
scanf("%c",&sexo);


if(sexo == 'm' || sexo == 'M'){
    pesoIdeal = (72.7 * altura) - 58;
} else{
    pesoIdeal = (62.1 * altura) - 44.7;
}

printf("O seu peso ideal eh %.2f kg \n",pesoIdeal);

system("pause");
return 0;
  • It’s probably garbage in the input buffer. Replace: scanf("%c",&sexo); for: scanf(" %c",&sexo);, note the space before the %c.

  • More details on item 12.26 of http://www.faqs.org/faqs/C-faq/faq/.

  • Solved the problem, thank you very much!

  • I don’t have the technical expertise to explain exactly what happened, but from my few experiences with C, I would venture to say that it may be a buffer error. Try using the command "fflush(stdin);" before the scanf, to "clean" the buffer. A hug and good luck! PS: I have dealt with Sos that had some problems with character data entries by the scanf command. If applicable, try the function "getchar()"; http://www.cplusplus.com/reference/cstdio/getchar/.

1 answer

1

These problems are common in the use of scanf(), and also occur when other input capture commands are used in conjunction with the same.

To make it clearer, let’s look at your code. The first time you make use of scanf() occurs on the following line:

scanf("%f",&altura);

Precisely in this passage a paragraph float is read from the standard input stdin and stored at the memory address pointed by the variable altura. In this reading, the scanf ignores the character \nwhich we have included in stdin the moment we squeeze ENTERon the keyboard to confirm the insertion of our number.

Thus, the line break character continues in the input buffer.

Next, the next use of the command occurs in:

scanf("%c",&sexo);

At that time, the input buffer stdinstill contains \n. The command reads this character immediately and naturally seems to "jump" to the next line instead of waiting for the user to type in some character and confirm its entry.

What happens then is that the scanf()reads an unwanted character, which is the \n, a second execution of the command.

To clear the input buffer, you can interlink calls from scanf() with a command getc(). Your show would look like this:

float altura, pesoIdeal = 0;
char sexo;

printf("Informe a sua altura : ");
scanf("%f",&altura);
getc(stdin); //getc(stdin) irá consumir o caracter `\n` do buffer de
             // entrada impedindo que interfira na captura de 
             //input posterior

printf("Informe o seu sexo : ");
scanf("%c",&sexo);


if(sexo == 'm' || sexo == 'M'){
    pesoIdeal = (72.7 * altura) - 58;
} else{
    pesoIdeal = (62.1 * altura) - 44.7;
}

printf("O seu peso ideal eh %.2f kg \n",pesoIdeal);

system("pause");
return 0;

Note that we have not assigned the result of getc()any variable because we are interested in discarding this value.

A more robust solution involving regular and more compact expressions can be found here.

Hugs!

Browser other questions tagged

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