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

S

Susan Rice

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

Wolfgang Meier

Susan said:
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
 
J

Jim Langston

Susan Rice said:
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.
 
F

Frederick Gotham

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();
}
 
O

osmium

Frederick Gotham said:
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.
 
S

Susan Rice

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?
 
J

Jakob Bieling

Susan Rice said:
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
 
M

Marcus Kwok

Jim Langston said:
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 <Enter> to continue...\n";
std::string trash;
std::getline(std::cin, trash);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top