Answer the question
In order to leave comments, you need to log in
How to fix the "while" loop so that one value is displayed ignoring the line break?
int main(void)
{
printf(" Type :");
char i= getchar();
while (i != 'A' && i != 'B' && i != 'C')
{
printf("\nYou choose incorrect type, please repeat...\nEnter r type :");
i= getchar();
}
}
Answer the question
In order to leave comments, you need to log in
Correct the line like this...
and see what happens in place of %c...
Can you explain the effect yourself?
And how to count a character in interactive work? I would write my own function that reads the character and everything that was buffered behind it right up to the line feed.
When you enter a character, you need to press <return> (Enter). So this one leads to the fact that the newline character \n is immediately sent after the entered character. In the next step of the loop, it is read and processed as a regular character, so a skip is obtained.
How it is solved: you need to make the input_char () function.
You need to read the string to the end, and then select the first character from the read string. You can use fgets() to read a string, and sscanf() can be used to read a character from the resulting character array. This is the fgets() + sscanf() way.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question