-4

I've recently read about how BAD system("PAUSE") is. I tried cin.get() but I don't know how to use it to pause a program. Other posts say to put a breakpoint after the statement. But I have no knowledge on how to do that. Any suggestions on how to pause my program? An example would be really appreciated.

Tacocat 4642
  • 13
  • 1
  • 1
  • 7
  • 1
    Why doesn't `cin.get()` work for you? – πάντα ῥεῖ Apr 22 '16 at 15:35
  • [`std::istream::get()`](http://en.cppreference.com/w/cpp/io/basic_istream/get) – πάντα ῥεῖ Apr 22 '16 at 15:41
  • 1
    `cin.get()` is a dumb way to pause your program anyway. If there's characters left in the input buffer, it won't pause at all. If there aren't characters in the buffer, it will only resume when you press enter, and any characters you input in the meantime will be displayed on the screen, resulting an a very unprofessional looking program. Which is fine if you're just making a program for yourself. But if that's case, `system("pause")` is fine too, if it works on your system. – Benjamin Lindley Apr 22 '16 at 15:44
  • ***Other posts say to put a breakpoint after the statement, but I have no knowledge on how to do that.*** In any version of Visual Studio navigate to the last statement in `main()` and press `F9` – drescherjm Apr 22 '16 at 15:45
  • Possible duplicate of [system("pause"); - Why is it wrong?](http://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong) – drescherjm Apr 22 '16 at 15:46
  • @BenjaminLindey I want to make my program cross-platform so it can run on my friend's Mac. – Tacocat 4642 Apr 22 '16 at 15:47
  • @πάνταῥεῖ Your example looks pretty good, can you show me in in a hello world program? – Tacocat 4642 Apr 22 '16 at 15:54
  • @Tacocat4642: Do you want your program to not be crap? Do you want to actually reproduce the behavior of `system("pause")`, where the user can just press any key to continue. – Benjamin Lindley Apr 22 '16 at 15:54
  • @BenjaminLindley haha, yes I do want my program not to be crap... – Tacocat 4642 Apr 22 '16 at 15:56
  • @Tacocat4642: Then the solution will not be quite so quick and easy. Standard C++ does not have a proper keyboard API. So you will either have to resort to platform specific function calls (for example, `GetAsyncKeyState` on Windows, and I don't know what on Mac). Or you will have to use a third party library that wraps those platform specific details for you (e.g. SDL or SFML). – Benjamin Lindley Apr 22 '16 at 16:10
  • 2
    We could repeat all the prior-given advice, or you could just read it where it is now. – Lightness Races in Orbit Apr 22 '16 at 16:37

2 Answers2

5

Assuming that you are indeed working on Windows, the worst thing about system("PAUSE") is that it betrays a fundamental misunderstanding of your operating system's architecture. You do not need a code replacement for system("PAUSE"), because the code is the wrong place to solve the perceived problem.

Beginners like to put system("PAUSE") or even a portable alternative like std::cin.get() at the end of a program because otherwise "the window disappears" as soon as the program ends. Such logic, however, is deeply flawed. The window which you probably see while the program runs and which has made you ask this question is not part of the program itself but part of the environment in which the program runs.

A typical console program, however, must not assume details about the environment in which it is executed. You must instead learn to think in more abstract terms when it comes to input and output via std::cout and std::cin. Who says that your program is even visible for a human user? You may read from or write into a file; you may use pipes; you may send text to a network socket. You don't know.

#include <iostream>

int main()
{
    std::cout << "Hello world\n"; // writes to screen, file, network socket...
}

Opening a graphical window and displaying text output on the screen is not in the scope of your program, yet using system("PAUSE") assumes exactly that one single use case and breaks all others.

If you use an IDE like Visual Studio and are annoyed by the fact that pressing F5 eventually results in the window disappearing before you have had the chance to see all output, here are three more sensible alternatives than manipulating the program itself:

  • Demystification. Observe that what Visual Studio really does is invoking the Visual C++ compiler behind the scenes in order to create an *.exe file. Open your own console window with cmd or with Tools > Visual Studio Command Prompt, locate the directory of that *.exe file and run it there (you should eventually also learn to start the compiler without Visual Studio's help, because that will give you a deeper understanding of the C++ build process).
  • Press CTRL+F5.
  • Place a breakpoint at the end of your code. Read the documentation if you don't know how.
Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
-4

I got one step closer, cin.ignore, but the user can only press enter if he/she wants to continue. Example with cin.ignore():

#include <iostream>
int main()
{
    cout << "Press enter to continue!\n";
    cin.ignore();
    //do something
    return 0;
}

When you press enter it advances and does whatever you want it to do.

Tacocat 4642
  • 13
  • 1
  • 1
  • 7