How to get window to stay open after C++ code runs?

user1174868 picture user1174868 · Jan 28, 2012 · Viewed 18.4k times · Source

Possible Duplicate:
How to stop C++ console application from exiting immediately?

I am trying to see my results, what do I do to my code so I can see if what I did is correct?

#include <iostream>
using namespace std;

int main()
{
    cout << "C++" << endl;
    cout << "The sum of 11 + 12 = " << 30/2 << endl;
    return 0;
}

Answer

MAK picture MAK · Jan 28, 2012

I think what you mean is that your DOS terminal closes as soon as your program ends.

A common solution is to have a call to cin, scanf or getch at the end of your program, just before your return 0. This forces the program to wait for some user input before exiting.

A better way is to compile your program and then run it from within a DOS prompt yourself. Just start up a DOS prompt, cd to the directory your program is in and run it from there.