Press enter to continue

J

junk mail

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.

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.

For example:-

do
{ /* Begin loop */
printf("\Press Enter"); /* Output to screen */
} while ((ch = getchar()) != '\n') ; /* While ( Condition -
must press enter ) */
 
J

Joona I Palaste

junk mail said:
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.

Can't be done in ISO standard C.
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.

ISO standard C can only read from stdin and write to stdout. It cannot
control anything about what happens to characters before they come to
stdin, or after they go to stdout.
Doing what your friend wants requires meddling with the characters
before the "read from keyboard" and "put into stdin" phases. This is
outside of C's scope and requires OS-specific code, which we don't
discuss here.
 
M

Mike Wahler

Joona I Palaste said:
Can't be done in ISO standard C.

#include <stdio.h>

int main()
{
int count = 0;

do
{
int c = 0;
count = 0;
printf("Press ENTER to continue...");
fflush(stdout);

while((c = getchar()) != '\n' && c != EOF)
++count;

if(count)
puts("ENTER only, I say!");

} while(count);

puts("Thank you.");
return 0;
}

:)


-Mike
 
J

Joona I Palaste

#include <stdio.h>
int main()
{
int count = 0;
do
{
int c = 0;
count = 0;
printf("Press ENTER to continue...");
fflush(stdout);
while((c = getchar()) != '\n' && c != EOF)
++count;
if(count)
puts("ENTER only, I say!");
} while(count);
puts("Thank you.");
return 0;
}

Which doesn't solve the problem of characters getting echoed to the
screen, so the code is useless.
 
M

Mike Wahler

Joona I Palaste said:
Which doesn't solve the problem of characters getting echoed to the
screen, so the code is useless.

I wasn't sure if inhibiting 'echo' was a strict requirement.
The code above does eliminate the problem of the 'extra'
characters getting submitted to subsequent inputs, which
is indeed a common complaint. But yes, you're right, no
way to inhibit the echo with standard code.

-Mike
 
D

Dan Pop

In said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top