I want to have a waiting or something like system("pause"), but it has been said to be evil. Is there any alternative? cin.get or cin.ignore do the purpose but I want it to be able to do any key combination, not just by pressing enter.
1. anything exactly like system("pause") but not using system();
2. also is there anything like system("CLS") but not using system();
To explain: the system("pause") is evil for a myriad of reasons, but in this case, it is specifically evil for distracting you from the true problem: incorrect program invocation.
Console programs are meant to be run from the console.
Yes, yes, I know that your IDE allows you to press Ctrl+F9 (or whatever) and run it directly. Some IDEs are even smart enough to keep the shell window open for you after your program has terminated. While playing with the IDE, go ahead and stick that PAUSE in there.
Just don’t forget to REMOVE IT before you give your program to anyone, like your professor.
I personally just open a Windows Console (tricked out with all my own configurations, but that’s a story for another day) and change to the directory that the IDE/Compiler put your program’s executable in. You can even get there from Explorer by navigating to the .EXE and clicking on the address bar and typing “cmd” and pressing Enter.
Thereafter, have your IDE compile your program without executing it, then switch to the Console Window and type your program’s name. And if you want to run it again (say, because you recompiled, or want to try with different inputs) then you only need to press the Up Arrow key and Enter. Easy peasy.
Keeping the console window open seems to be something that only Windows users are concerned with. I guess it's because on Windows when you double click on the executable file it will automatically open a console window that is automatically closed when the program terminates.
What happens on other systems when double clicking on an executable file is usually that it will run the program but it will not automatically open anything to show output or accept input. Users on these systems are more used to console programs and knows how to run them from the console. For a program that is meant to be run from the console it would just be irritating if it didn't terminate when it was finished but instead asked for more (useless) input.
The point that I'm trying to make is that if you insist that the user should have to press a key before the program terminates you probably only want this on Windows so it might be acceptable to use a solution that is Windows-specific.
If you decide to use cin.ignore() (or cin.get()) note that it will only wait for more input if the input buffer is empty and none of the error flags are set. To clear the error flags you can use cin.clear(). To make sure the input buffer is empty you can use cin.sync() (this seems to only work on Windows).
1 2 3 4 5 6
cout << "Press enter to exit.";
For some programs it can make sense to force the user to take an active step in order to close the program. In a game for instance you could ask the user if he want's to play again. If the answer is yes you just repeat, otherwise you just let the program terminate immediately. This makes the whole issue go away and it works fine everywhere but it's not suitable for all types of programs.
If you want more advanced control over the console window you might want to use the WinAPI or a library such as pdcurses. This would allow you to do things such as reading key presses directly without having to press enter. It would also provide you with simple functions for clearing the whole screen and for printing things at any position you like. You can even do double buffering to avoid flicker when the screen is updated.
Isn't it a bit confusing to have a function named PAUSE() if it doesn't always pause? I think it should either be renamed to something like pause_on_windows(), or only defined and called when compiling the program on windows.
The last time this was brought up, the person made a solution to put the pause functionality inside the destructor of an object made in main, pretty creative... but so futile, it's like giving a bandage to someone suffering from a heat stroke. I'm not really adding anything more than that Duthomhas has said, but I wanted to reiterate that it isn't just some IDEs, but pretty much every modern IDE that has a way of either stopping the console, or they have their own space for input/output (like Eclipse).
And if you're not working through an IDE, having a pause is a disadvantage -- so it's best to keep that pause separate from the program logic itself. For example, you could write a simple batch file that just looks like:
all the alternatives require pressing multiple keys because of the blasted buffered IO or the solutions become insanely complex to resolve that issue. All to replace 1 line of code that people want to avoid on principle over practical.
Or to put it another way, until an unbuffered getch() is standard, there is a gap that pause fills.
Your conflict of thinking prejudices yourself into claiming incorrect things and reveals your indifference to the actual issue. One, that has, not-so-incidentally, bothered people for the last 20+ years. At least.
If I were to guess what the pause function of the cmd.exe looked like, I bet it would look something similar to this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
constchar* prompt = " Press any key to continue . . .";
// Print the user prompt
WriteFileA( hConsoleOutput, prompt, lstrlenA( prompt ), &dwNumberOfCharsWritten, NULL );
// Wait for the user to press any key and consume it
FlushConsoleInputBuffer( hConsoleInput );
WaitForSingleObject( hConsoleInput, INFINITE );
FlushConsoleInputBuffer( hConsoleInput );
Hmm, looks awfully familiar...
And dang if it isn’t short and easy to do! (One liners are for script kiddies.)