473,236 Members | 1,614 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,236 software developers and data experts.

Impliment "Press any key to continue" in console program?

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 read whatever messages there are before the console window
disappears.
Jul 4 '06 #1
9 64175
try getch, getche, system("PAUSE")

-- Murali Krishna

Jul 4 '06 #2
Susan Rice wrote:
What I want is the program to pause before exiting so the user
can read whatever messages there are before the console window
disappears.
Depending on who uses your program and what it is used for, you might
not want to *force* your users to do that. They could want to run it
non-interactively, e.g. in a batch process.

Consider using a wrapper script that calls your program, then the
"pause" command or point out the possibility (for Windows users) to
alter the "Properties Program Close on exit" setting.

Regards,

Wolf

Jul 4 '06 #3
"Susan Rice" <sr****@cox.netwrote in message
news:8upqg.1030$lv.796@fed1read12...
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 read whatever messages there are before the console window
disappears.
Unfortunately, there is no cross platform way to do it. You can use an OS
specific call ( while ( !kbhit(), getch(), system("pause") etc.. in
windows ) or use a 3rd party library with such a call ( I seem to recall
boost has one ). The 3rd party libraries, as I'm aware, just take the their
function and call the OS specific code anyway.

If you are not concerned with cross platform operability, then use an OS
specific call.

What I do is the following code which is cross platform:

std::string wait;
std::cin >wait;

but this requires 2 key strokes. Any key and enter. Enter alone won't do
it. Any other key alone won't do it.

If your problem is with VC++ closing when you run a program before you can
see the output, one solution is to press ctrl-f5 to turn it instead of F5,
then it waits for a keypress before it closes the console.
Jul 4 '06 #4
Susan Rice posted:
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 read whatever messages there are before the console window
disappears.

include <cstdio>

int main()
{
std::puts("Press any key to continue...");

std::getchar();
}
--

Frederick Gotham
Jul 4 '06 #5
"Frederick Gotham" writes:
include <cstdio>

int main()
{
std::puts("Press any key to continue...");

std::getchar();
}
That puts a new spin on "any' that even Microsoft hasn't stooped to use.
Jul 4 '06 #6
How about if I say Press RETURN to exit. Will this work OK?

void MyExit()
{
printf( "Press RETURN to finish:" );
char c = getchar();
exit(1);
}

What I'm finding is I can enter a bunch of characters before pressing
RETURN, and I'm wondering what happens to all those characters.
Does just the first character go into c, and the rest are discarded?
There's no buffer overflow happening is there?

Frederick Gotham wrote:
Susan Rice posted:

>>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 read whatever messages there are before the console window
disappears.

include <cstdio>

int main()
{
std::puts("Press any key to continue...");

std::getchar();
}

Jul 5 '06 #7
Susan Rice wrote:
How about if I say Press RETURN to exit. Will this work OK?

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the newsgroup FAQ:

<http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4>


Brian

Jul 5 '06 #8
Susan Rice <sr****@cox.netwrote:
How about if I say Press RETURN to exit. Will this work OK?

void MyExit()
{
printf( "Press RETURN to finish:" );
char c = getchar();
exit(1);
}

What I'm finding is I can enter a bunch of characters before pressing
RETURN, and I'm wondering what happens to all those characters.
Does just the first character go into c, and the rest are discarded?
There's no buffer overflow happening is there?
The rest will remain in the input buffer and read by the next
request for input. In your case, there is no "next request" so the
remaining input will be discarded.

hth
--
jb

(reply address in rot13, unscramble first)
Jul 5 '06 #9
Jim Langston <ta*******@rocketmail.comwrote:
"Susan Rice" <sr****@cox.netwrote in message
news:8upqg.1030$lv.796@fed1read12...
>What I want is the program to pause before exiting so the user
can read whatever messages there are before the console window
disappears.

What I do is the following code which is cross platform:

std::string wait;
std::cin >wait;

but this requires 2 key strokes. Any key and enter. Enter alone won't do
it. Any other key alone won't do it.
I usually use:

std::cout << "Press <Enterto continue...\n";
std::string trash;
std::getline(std::cin, trash);

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 11 '06 #10

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

Similar topics

3
by: Harman Sahni | last post by:
As per this URL http://msdn.microsoft.com/library/en-us/vjref98/html/14_14.asp?frame=true conitnue works on for, while, do... I know it works for foreach as well as I'm using it somewhere. My...
2
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...
5
by: Matt | last post by:
I will be grateful if someone explians this part colfree = FALSE; upfree = FALSE; downfree = FALSE; of the code below. I don't understand how this marks the downward and upward diagonals....
0
by: Noam_h | last post by:
Hi all. Calling this web service: POST /WebService2/Service1.asmx HTTP/1.1 Host: WKSI105080A22 Content-Type: text/xml; charset=utf-8 Content-Length: 396 SOAPAction:...
8
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...
1
hpbutterbeer
by: hpbutterbeer | last post by:
We have a Machine Project and my brain is currently in a clouded state. Sorry, I'm just a beginner in C Programming... Text twist is a windows game whose main objective is to form words out of the...
3
by: per9000 | last post by:
Hi, can I print some default text on the console when doing a ReadLine? A silly example below shows two small scenarios. In the first a user is told what he appears to be called, and asks for a...
2
by: Chris Thomasson | last post by:
I was wondering if the 'SLINK_*' and 'SLIST_*' macros, which implement a simple singly-linked list, will produce _any_ possible undefined behavior: ____________________________ #include...
0
by: nstout | last post by:
writing a program in java System.out.print("Continue? (y/n): "); choice = sc.next(); System.out.println(); Then it displays "press any key to continue..." How do you stop this from...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.