I am new to C, so please forgive me if this question is stupid or strange asked.
I am reading C primer plus, and one example in chapter 8 is a loop that checks if a user is entered - a newline character or notwhich I could not understand.
The code is short, so I will show it to you:
int main(void)
{
int ch;
int rows, cols;
printf("Enter a character and two integers:\n");
while ((ch = getchar()) != '\n')
{
if (scanf("%d %d",&rows, &cols) != 2)
break;
display(ch, rows, cols);
while (getchar() != '\n')
continue;
printf("Enter another character and two integers;\n");
printf("Enter a newline to quit.\n");
}
printf("Bye.\n");
return 0;
}
void display(char cr, int lines, int width)
What I don't understand is here:
while (getchar() != '\n')
continue;
printf("Enter another character and two integers;\n");
printf("Enter a newline to quit.\n");
First of all, while (getchar() != '\n')checks that the first ch was entered correctly? Secondly, if this is so, then why does the continuation not skip the printf statements and move on to the first? Isn't that what he should do?
Tpx
source
share