By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,889 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,889 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
try getch, getche, system("PAUSE")

-- Murali Krishna

Jul 4 '06 #2

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.