Public Group

# Pausing programs

This topic is 4369 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
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]

##### Share on other sites
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

##### Share on other sites
Either run it with cmd.exe or put this at the bottom:
std::cin.ignore(std::cin.rdbuf()->in_avail() + 1);

##### Share on other sites
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.

##### Share on other sites
Oh god, not this again. Can we put it in the FAQ already?

cin.get();
worked great.

##### Share on other sites
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>.

##### Share on other sites
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]

##### Share on other sites
Quote:
 Original post by Kazgorothcin.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.

• 11
• 20
• 12
• 10
• 35
• ### Forum Statistics

• Total Topics
631399
• Total Posts
2999857
×