Press any key to continue . . . |
This is typically a problem on Windows, caused by really dumb IDEs that don't know enough to keep the console open after the program finishes. However, it does strike right at one of the main philosophical problems with
the way you are thinking about programming. After all, a console program should be run from the
console --else once it terminates, the console
should disappear. We'll get to fixing that later. For now, we'll stick with the immediate problem.
- - - - - - - - - - - - - - - - The Simple Answer - - - - - - - - - - - - - - - - |
While simple, it really is a
Bad Thing. See
Why system() is evil for more.
1 2 3 4 5 6 7
|
#ifdef __cplusplus__
#include <cstdlib>
#else
#include <stdlib.h>
#endif
system("PAUSE");
| |
It is also Windows-only. To do the same thing in Linux, you'd need to use the
read shell command
1 2
|
system("read -p \"Press a key to continue...\" -n 1 -s");
| |
- - - - - - - - - - - - - - - - The Standard Way - - - - - - - - - - - - - - - - |
The most correct way to do it is something like:
1 2 3 4 5 6 7 8
|
#include <iostream>
#include <limits>
void PressEnterToContinue()
{
std::cout << "Press ENTER to continue... " << flush;
std::cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );
}
| |
1 2 3 4 5 6 7 8 9
|
#include <stdio.h>
void PressEnterToContinue()
{
int c;
printf( "Press ENTER to continue... " );
fflush( stdout );
do c = getchar(); while ((c != '\n') && (c != EOF));
}
| |
Thereafter, simply place a call to it at the end of your program:
1 2 3 4 5 6 7 8
|
#include <stdio.h>
int main()
{
puts( "Hello world!" );
PressEnterToContinue();
return 0;
}
| |
The main problem with this approach is that
it only works when input is properly synchronized. If it isn't, you will need to
Flush Stdin. Learn more about proper synchronization with
Issues When Reading User Input.
Also, it requires the user to press the
Enter key to continue. This isn't such a bad problem as you might think. The mythical
Any key has befuddled a lot of people. (A better phrase would have been "Press a key to continue..." --at worst people will press the
A key.) Remember,
non-programmers are almost always your target audience. And even if they aren't, what you are thinking isn't often as obvious as you think. Make sure to give clear instructions to the user about what kind of input your program wants next.
- - - - - - - - - - - - - - - - Using NCurses - - - - - - - - - - - - - - - - |
The Curses library is designed for working with the console. Advantages: it is cross-platform. Disadvantages: it doesn't interact well with the standard streams. In other words, you shouldn't mix
printf(), etc or
cout, etc with curses. Use one or the other, not both.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include <curses.h>
int main()
{
initscr();
addstr( "Hello world!\n\n" );
addstr( "\nPress a key to continue..." );
cbreak(); /* turn off line-buffering */
noecho(); /* turn off character echo */
getch(); /* read and discard a single character (caveats!) */
echo(); /* turn echo back on */
nocbreak(); /* turn line-buffering back on */
endwin();
return 0;
}
| |
There are some other things you must be aware of when using [
w]
getch(). Make sure you read through
Getting Started with Curses.
So if all you want to do is pause your program once or twice then using Curses is overkill.
- - - - - - - - - - - - - - - - Using <conio.h> - - - - - - - - - - - - - - - - |
Well, first some code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <conio.h>
#include <stdio.h>
void PressAKeyToContinue()
{
int c;
printf( "\nPress a key to continue..." );
c = getch();
if (c == 0 || c == 224) getch();
}
int main()
{
puts( "Hello world!" );
PressAKeyToContinue();
return 0;
}
| |
This library is severely deprecated, but it is so popular that some form of it exists on most 80x86 hardware compilers --almost always on Windows compilers, and there exist Linux versions too. However, given the choice, use
NCurses instead...
The caveat is that it is
non-standard, meaning that the actual functions it provides vary a lot and they don't always behave just right. (Microsoft invented it years ago. That's right,
not Borland --though Borland is [in]famous for it).
Hence, for anything other than Windows programs it is also a sub-optimal solution. See
Using <conio.h> for more.