473,569 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Press enter to continue

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 ) */
Nov 14 '05 #1
5 20712
junk mail <mi**********@n tlworld.com> scribbled the following:
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.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"No, Maggie, not Aztec, Olmec! Ol-mec!"
- Lisa Simpson
Nov 14 '05 #2

"Joona I Palaste" <pa*****@cc.hel sinki.fi> wrote in message
news:c1******** *@oravannahka.h elsinki.fi...
junk mail <mi**********@n tlworld.com> scribbled the following:
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.


#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
Nov 14 '05 #3
Mike Wahler <mk******@mkwah ler.net> scribbled the following:
"Joona I Palaste" <pa*****@cc.hel sinki.fi> wrote in message
news:c1******** *@oravannahka.h elsinki.fi...
junk mail <mi**********@n tlworld.com> scribbled the following:
> 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.

#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.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Shh! The maestro is decomposing!"
- Gary Larson
Nov 14 '05 #4

"Joona I Palaste" <pa*****@cc.hel sinki.fi> wrote in message
news:c1******** **@oravannahka. helsinki.fi...
Mike Wahler <mk******@mkwah ler.net> scribbled the following:
"Joona I Palaste" <pa*****@cc.hel sinki.fi> wrote in message
news:c1******** *@oravannahka.h elsinki.fi...
junk mail <mi**********@n tlworld.com> scribbled the following:
> 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.

#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.


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
Nov 14 '05 #5
In <sU70c.929$GQ.8 83@newsfe1-win> "junk mail" <mi**********@n tlworld.com> 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: Da*****@ifh.de
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
71909
by: JCO | last post by:
How's come when I press the enter key, I can't get it to execute the correct password. It seems that I'm forced to press the button. I want to be able to do both. How is this done?
2
4513
by: Paul Johnston | last post by:
I'm using VB.Net. I've tried using Console.Read() but it requires a line-terminator before it finishes (doesn't that mean it's functionally equivalent to ReadLine() -- why have it then?). I've tried Console.In.ReadBlock(), too, with no success. So, along these lines, how would I implement, simply, a "Press Any Key to Continue" routine or,...
9
5272
by: JuanK | last post by:
hello, i'm trying to read a character from console just like getc function in c languaje i'm trying with WINAPI but dont works at this time.. other methods like clear screen works OK with the WINAPI.. an others to please helpe and try understandme because i dont speak in english tx
6
2463
by: Bob Altman | last post by:
Hi all, I have an unmanaged console app written in C++. I can't figure out how to get C++ to do the standard issue "press any key to continue" thing: cout << "Press any key to continue: "; cin >> <your code goes here>; TIA,
8
12860
by: Timur Ametov | last post by:
Hallo everybody. I'm using Visual Studio 7 and hier is my question. How can i see in Outputwindow after my Programm ended the message "Press any key to continue". Is it possible? Now this window will closed automatisch. Thank you
9
64342
by: Susan Rice | last post by:
I'm running a simple win32 console application and I want to impliment a "Press any key to continue", so I print that prompt, and then what's the easiest way to impliment reading any key? Do I use 'getchar', or 'gets', or some get function? or 'cin', or what's an easy way? What I want is the program to pause before exiting so the user can...
4
2613
by: kotoro | last post by:
I'm in an intro to computer programming course and just for the purposes of my test drivers and personal implementation, I would like to know if it is possible to force the program to wait for the enter key before proceeding without allowing the user to type in anything. I don't really have any experience with the buffered reader, we were provided...
2
2054
by: Greoasy | last post by:
I have a registration page with no of text boxes and 3 buttons for insert update and delete. and a logout button on the top of the page. If i place my control in an of the text box and press enter key it automatically goes to the logout button and it logouts. Actually when i press the enter key it should avctivate either of the insert,update...
1
6983
by: daonho | last post by:
I tried to use javascript to trigger up the button click function when user press enter key from the textbox. This function work fine with a single button click such has login page. However, if the page has multiple button such login page with a search function somewhere around, then it's not respond properly. I have attached a brief example of...
0
7701
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7615
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7677
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.