469,626 Members | 1,361 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

Using Enter key as user-controlled program flow

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

Feb 4 '07 #1
18 1915
Andrew Gentile wrote:
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);
Feb 4 '07 #2
In article <11*************@news-west.n>,
Christopher Layne <cl****@com.anodizedwrote:
>Andrew Gentile wrote:
>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);
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.

Feb 4 '07 #3
Andrew Gentile wrote:
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.

Feb 4 '07 #4
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);
Feb 4 '07 #5
Christopher Layne wrote:
To be "safe":
Correction: "to even work"
Feb 4 '07 #6
thanks...

On Feb 4, 6:56 am, Christopher Layne <cla...@com.anodizedwrote:
Andrew Gentile wrote:
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);- Hide quoted text -

- Show quoted text -

Feb 4 '07 #7
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

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 4 '07 #8
Richard Heathfield wrote:
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
while (getchar() != '\n') ;

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Feb 4 '07 #9
"Andrew Gentile" <an************@gmail.comwrites:
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.

--
Ben.
Feb 4 '07 #10
Joe Wright said:
Richard Heathfield wrote:
>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

while (getchar() != '\n') ;
You forgot about the possibility of EOF.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 4 '07 #11
In article <6-******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>while (getchar() != '\n') ;

You forgot about the possibility of EOF.
The user will eventually find the interrupt key.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Feb 4 '07 #12
Andrew Gentile wrote:
Christopher Layne <cla...@com.anodizedwrote:
>Andrew Gentile wrote:
>>>
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?

char n;
fgets(&n, 1, stdin);- Hide quoted text -

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

Feb 4 '07 #13
On 4 Feb, 14:09, Christopher Layne <cla...@com.anodizedwrote:
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.
--
Nick Keighley

Feb 5 '07 #14
"Nick Keighley" <ni******************@hotmail.comwrites:
On 4 Feb, 14:09, Christopher Layne <cla...@com.anodizedwrote:
>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.
Feb 5 '07 #15
In article <87************@gmail.com>, Richard <rg****@gmail.comwrote:
>"Nick noodles-for-brains" <ni******************@hotmail.comwrites:
....
>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.
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.

Feb 5 '07 #16
Richard said:
"Nick Keighley" <ni******************@hotmail.comwrites:
<snip>
>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.
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 5 '07 #17
Richard Heathfield wrote:
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 :).

Feb 5 '07 #18
Christopher Layne said:
Richard Heathfield wrote:
>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 :).
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:
<eq**********@news.xmission.com>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 5 '07 #19

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by ewillyb | last post: by
2 posts views Thread by rn5a | last post: by
5 posts views Thread by Mukesh | last post: by
8 posts views Thread by =?Utf-8?B?TGlzYUNvbnN1bHQ=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.