In <sU70c.929$GQ.883@newsfe1-win> "junk mail" <> writes:
>My friend is trying to code a small c program where he wants to force the
>user to press enter and only enter to continue.
He's lucky, because Enter/Return is the only key for which a standard
solution exists.
>Currently he is using getchar() with a loop but you can type any number of
>characters, which are echoed to screen before you have to press enter.
So what? The program is still guaranteed to wait until Enter is pressed.
There is no way to prevent the user from pressing anything he wants
(short of giving him a keyboard with a single key), but the program can
still wait until the Enter key is pressed and discard everything, the
Enter key included.
>For example:-
>
>do
> { /* Begin loop */
> printf("\Press Enter"); /* Output to screen */
> } while ((ch = getchar()) != '\n') ; /* While ( Condition -
>must press enter ) */
In such cases you may also want to detect the user typing the eof key,
and handle this case separately. Otherwise, you may end up waiting for
an Enter key that will *never* come.
int rc = scanf("%*[^\n]");
if (rc < 0) /* the eof key was pressed */ ;
else getchar(); /* remove the newline character from stdin */
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: