Pausing programs

Started by
8 comments, last by nullsquared 17 years, 9 months ago
I have this program I'm working on and I want it to pause until the user presses a button, how would I do this?
Advertisement
There is two ways of doing this.
depends on which one you like better. Test em' out! [smile]

ex 1:
cout << "HELLO\n\n";system("pause");cout << "WORLD\n\n";system("pause");


ex 2:
cout << "HELLO\n\n";cout << "Press Enter to exit.";cin.get();


Both basically do the samething. Hope that helps [smile]
Hi
Usually if I have a loop I use states, one state could be pause in wich I only check for user input and on keypress changes to another state which will continue doing what ever it is it should do.



Scanmaster_K
Either run it with cmd.exe or put this at the bottom:
std::cin.ignore(std::cin.rdbuf()->in_avail() + 1);
Console I'm assuming? You can try the function "getch()", or "getche()" if you want the character pressed to echo. It's defined in conio.h. Hope that helps. :)


-Etyrn

Edit: Holy, I was beating by a long shot.
Oh god, not this again. Can we put it in the FAQ already?
cin.get();
worked great.
Nuclear Rabbit - the problems with advising people to use system("pause") are twofold:

1) It is not portable from OS to OS

and 2) (much more important) Once the program is compiled, if someone malicous puts another executable called PAUSE.EXE in the same directory as the program, when the program runs it will execute this exe instead of the command.com version, presumabley with the same permissions as the original program.

The majority of compilers support getch() through <conio.h>.
You shouldn't really do this at all because a) you're abusing a function in order to achieve the pause and b) pausing after execution isn't really desired behavior from a real console application, which should perform it's tasks, return any values and then exit. Console applications are often used to perform simple tasks and can be batched in order to perform the operation in question multiple times (for instance, performing some alteration to every file in a directory) but won't be able to do so without a user present pressing enter for every file if a pause is used.

Of course that's all well and good, but you still want to see your program output, right? Well, there's a couple of solutions for that:
- It's a console program; run it from the console! Under Windows you can get a command line by clicking on Start>Run and typing "cmd" into the box. Navigate to the folder in which your program is stored and run it.
- In Visual Studio use the "start without debugging" option found on the debug menu, or use the Ctrl+F5 shortcut.

cin.get() should do it for you though, just don't use it if you ever get around to writing a 'real' console application. [wink]

- Jason Astle-Adams

Quote:Original post by Kazgoroth
cin.get() should do it for you though, just don't use it if you ever get around to writing a 'real' console application. [wink]


Like I said in some other thread (more recent one, but only by 1 day), cin.get() will NOT block if there is anything left in the buffer. std::cin.ignore(std::cin.rdbuf()->in_avail() + 1); will.

This topic is closed to new replies.

Advertisement