About getchar() function / scanf() function

Asked 2 years ago, Updated 2 years ago, 147 views

int main(void) { int evil1, evil2; char ch;

do {
    printf("Enter a pair of integers:\n");
    scanf("%d %d", &evil1, &evil2);
    printf("The lesser of %d and %d is %d.\n", evil1, evil2, (evil1 < evil2) ? evil1 : evil2);
    printf("Press 'y' to continue,or 'q'to quit.");
    while((ch = getchar()) == '\n'); // I don't understand...


} } while (ch == 'y');
printf("Bye.\n");
return 0;

}

while ((ch = getchar()) == '\n'); I don't understand why the code is written like this here.ㅠ<

getchar

2022-09-21 21:07

1 Answers

Pressing the Enter key is to escape the loop. I think the while door was used twice.

It is called a new line character, so it is marked with "\n".


2022-09-21 21:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.