Is there a decent wait function in C++?

user98188 picture user98188 · May 23, 2009 · Viewed 352.4k times · Source

One of the first things I learned in C++ was that

#include <iostream>
int main()
{
    std::cout<<"Hello, World!\n";
    return 0;
}

would simply appear and disappear extremely quickly without pause. To prevent this, I had to go to notepad, and save

helloworld.exe
pause

ase

helloworld.bat

This got tedious when I needed to create a bunch of small test programs, and eventually I simply put while(true); at the end on most of my test programs, just so I could see the results. Is there a better wait function I can use?

Answer

DeadHead picture DeadHead · May 23, 2009

you can require the user to hit enter before closing the program... something like this works.

#include <iostream>
int main()
{
  std::cout << "Hello, World\n";
  std::cin.ignore();
  return 0;
}

The cin reads in user input, and the .ignore() function of cin tells the program to just ignore the input. The program will continue once the user hits enter.

Link