473,395 Members | 1,418 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 2139
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Peloux | last post by:
Hi, I have written some htc in order to validate data in a form. most of htc are attached on 'onblur' event. Now, we would like to use the Enter Key to sublit form, so we use the following...
32
by: Protoman | last post by:
I have a function that calculates the mean of the some numbers; I need it to accept any number of parameters (one needs to be the number of the other parameters) Can you help me out here? Here's...
3
by: Pete | last post by:
I'm currently doing a database that uses comboboxes to look up records in other tables, whether they be lookup tables or otherwise. When a user needs to add an item to one of these tables, the...
5
by: ewillyb | last post by:
Hi, ASP.NET has some interesting behavior when the user hits the Enter key. If there are multiple ASP:Buttons (rendered as HTML submits) on the form, when the user hits enter, the first button's...
5
by: fkater | last post by:
Hi, we need to enter lots of data which are basicly descriptions of pc hardware configuration like this: Mainboard: vendor=... #of PCI slots=3 slot 1 graphic adapter
2
by: rn5a | last post by:
I have created the following WebService named NConnect.asmx using which I want an ASPX page to first authenticate a user & after successful user validation, the ASPX page should display a few...
5
by: Mukesh | last post by:
Hi i want to use AJAX.net in my Existing Application I have already installed the ajax .net ..net 3.0 and using VS 2005 in the old application i have added a new web form then script manager...
8
by: =?Utf-8?B?TGlzYUNvbnN1bHQ=?= | last post by:
I have a data entry application. Due to ease and location, the users utilizie the enter key to move from field to field, rather than the tab key. Depending on the data they enter into a field,...
1
Carrugar
by: Carrugar | last post by:
Hi folks, I am issuing the net use command on a couple of Win2003 servers. Some of these servers require that the name and password be authenticated twice and other servers just once, i.e.: ...
1
by: JFKJr | last post by:
Hello everyone, the following Access VBA code opens an excel file and creates textboxes in a given range of cells dynamically. The code attaches "MouseUP" and "Exit" events to the textboxes (using...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.