Monday, April 12, 2021

-- How to introduce a pause in your C++ program --

 I often get asked by my students how to introduce a pause on their C++ program, the problem is that depending of the IDE you are using, after your program executes is closes the command line window automatically, so here are my 2 favorite approaches.

 Windows Operating System


#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    cout << "Please press any key to continue";
    system("pause");
    return 0;
}

If you are using either Linux / Unix unfortunately the pause command does not work, but you can use something like this


#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    cout << "Please press any key to continue";
    cin.ignore();
    return 0;
}


 

No comments: