Using Enter key as user-controlled program flow

A

Andrew Gentile

Hello,
I would like to find a way of using scanf() and the Enter key to
have user-controlled program flow. Currently, I have a couple of
lines in my program which serves as a pause in the program.


printf("\n\tTo continue, press any key followed by Enter: ");
scanf("%i",&temp);

This works as a program pause, but it requires that the user enter a
key plus enter. Is there a way to use scanf() so that the user only
needs to use the Enter key? Or is there a better function than
scanf() for this purpose?

thanks,
Andrew
 
C

Christopher Layne

Andrew said:
Hello,
I would like to find a way of using scanf() and the Enter key to
have user-controlled program flow. Currently, I have a couple of
lines in my program which serves as a pause in the program.


printf("\n\tTo continue, press any key followed by Enter: ");
scanf("%i",&temp);

This works as a program pause, but it requires that the user enter a
key plus enter. Is there a way to use scanf() so that the user only
needs to use the Enter key? Or is there a better function than
scanf() for this purpose?

thanks,
Andrew

char n;
fgets(&n, 1, stdin);
 
K

Kenny McCormack

char n;
fgets(&n, 1, stdin);

I'm sure the professional nitpickers will have a thing or two to say
about that (how it could fail, why it's not portable, etc), you have to
admit that the real, canonical, answer to the OP's question is (as
always):

Off topic. Not portable. Cant discuss it here. Blah, blah, blah.
 
S

santosh

Andrew said:
Hello,
I would like to find a way of using scanf() and the Enter key to
have user-controlled program flow. Currently, I have a couple of
lines in my program which serves as a pause in the program.


printf("\n\tTo continue, press any key followed by Enter: ");
scanf("%i",&temp);

This works as a program pause, but it requires that the user enter a
key plus enter. Is there a way to use scanf() so that the user only
needs to use the Enter key? Or is there a better function than
scanf() for this purpose?

Though you can use scanf() for this purpose, a simpler function like
getchar(), getc() or fgetc() would be more suitable.

int c;
fputs("\n\tTo continue, press Enter...\n", stdout);
c = getc(stdin);

You'll have to ensure that there're no characters pending in the input
stream's buffers. Otherwise, the call to getc() will read those,
instead of blocking for input from the keyboard.
 
C

Christopher Layne

Kenny said:
I'm sure the professional nitpickers will have a thing or two to say
about that (how it could fail, why it's not portable, etc), you have to
admit that the real, canonical, answer to the OP's question is (as
always):

Off topic. Not portable. Cant discuss it here. Blah, blah, blah.

To be "safe":

char n[2];
fgets(n, sizeof n, stdin);
 
R

Richard Heathfield

Andrew Gentile said:
Is there a way to use scanf() so that the user only
needs to use the Enter key? Or is there a better function than
scanf() for this purpose?

getchar
 
B

Ben Bacarisse

Andrew Gentile said:
Hello,
I would like to find a way of using scanf() and the Enter key to
have user-controlled program flow. Currently, I have a couple of
lines in my program which serves as a pause in the program.


printf("\n\tTo continue, press any key followed by Enter: ");
scanf("%i",&temp);

This works as a program pause, but it requires that the user enter a
key plus enter. Is there a way to use scanf() so that the user only
needs to use the Enter key? Or is there a better function than
scanf() for this purpose?

....another option (you've had several already) is:

scanf("%*1[\n]")

which reads just one newline (and does not put it anywhere). It won't
read anything it shouldn't and it does not need any variables.
 
C

CBFalconer

Andrew said:
thanks...

Please do not top-post. Your answer belongs after the quoted
material.

Try the following:

int flushln(FILE *f) {
int ch;

while ((EOF != (ch = getc(f))) && (ch != '\n')) continue;
return ch;
}

/* ---------------- */

...
printf("\n\tTo continue, press Enter: ");
fflush(stdout);
flushln(stdin);

after ensuring you have #include <stdio.h>. In general scanf is
very troublesome for interactive input, unless you use it for only
one item at a time, and check its return value is exactly 1.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
 
N

Nick Keighley

Kenny McCormack wrote:
I'm sure the professional nitpickers will have a thing or two to say
about that (how it could fail, why it's not portable, etc), you have to
admit that the real, canonical, answer to the OP's question is (as
always):
Off topic. Not portable. Cant discuss it here. Blah, blah, blah.

To be "safe":

char n[2];
fgets(n, sizeof n, stdin);

Kenny is a troll. He just says silly things to get a response.
It is best to ignore him.
 
R

Richard

Nick Keighley said:
Kenny McCormack wrote:
char n;
fgets(&n, 1, stdin);
I'm sure the professional nitpickers will have a thing or two to say
about that (how it could fail, why it's not portable, etc), you have to
admit that the real, canonical, answer to the OP's question is (as
always):
Off topic. Not portable. Cant discuss it here. Blah, blah, blah.

To be "safe":

char n[2];
fgets(n, sizeof n, stdin);

Kenny is a troll. He just says silly things to get a response.
It is best to ignore him.

Actually, he made a very valid point. And your pointing out of trolls is
OT.
 
K

Kenny McCormack

Actually, he made a very valid point. And your pointing out of trolls is
OT.

Well said, sir!

In fact, I think the next time one of these noodle brains makes one of
their pansy "Kenny is a troll" posts, I'll need to point out that they
have committed the grave sin of OT.
 
R

Richard Heathfield

Richard said:
"Nick Keighley" <[email protected]> writes:

Actually, he made a very valid point. And your pointing out of trolls
is OT.

No, he didn't make a very valid point. The OP's problem has a portable
solution, and he was quite wrong to claim otherwise.

As for "pointing out of trolls is OT", that's true, but it's best to
consider it part of the (unavoidable) overhead. Topicality is designed
to protect the group from fragmentation and chaos. Off-topic articles,
if not dealt with, lead to fragmentation, but at least it's (normally)
only because the OP doesn't know better. Trolls *deliberately* attempt
to cause chaos. So, when a group grows trolls, it's useful to the group
for newcomers to be warned of their existence, for the same reason that
it's useful to the group that it should frown upon OT articles.
 
C

Christopher Layne

Richard said:
No, he didn't make a very valid point. The OP's problem has a portable
solution, and he was quite wrong to claim otherwise.

"He would have been quite wrong to claim otherwise" - you mean. I realized the
error and repented for a minimum of 72 hours Richard :).
 
R

Richard Heathfield

Christopher Layne said:
"He would have been quite wrong to claim otherwise" - you mean. I
realized the error and repented for a minimum of 72 hours Richard :).

In my article, "he" referred not to you but to Kenny McCormack. It was
he who wrongly claimed that the OP's question was off-topic. Reference:
<[email protected]>
 

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,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top