Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Press Enter to Continue in C

Tags:

c

input

How can you do a "Press Enter to Continue" in C?

like image 306
Smashery Avatar asked Sep 10 '09 16:09

Smashery


People also ask

How do you take input until Enter is pressed in C?

We should use "%[^\n]", which tells scanf() to take input string till user presses enter or return key. Scanf scans till user presses enter or till user presses return key.

How do you get an Enter from the user in C?

In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyboards.


2 Answers

printf("Press Enter to Continue");
while( getchar() != '\n' );

A check for '\r' is nice for ultimate portability, but really only matters if you are targeting Mac OS v9 or older (OS-X, Unix & Windows all use either '\n' or, for windows, '\r\n')

like image 58
Eric Petroelje Avatar answered Jan 01 '23 21:01

Eric Petroelje


printf("Press enter to continue\n");
char enter = 0;
while (enter != '\r' && enter != '\n') { enter = getchar(); }
printf("Thank you for pressing enter\n");
like image 25
Dave DeLong Avatar answered Jan 01 '23 21:01

Dave DeLong