0

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

I am running a simple program, written in Dev C++ 4.9.9.2 IDE in Windows 7:

// my second program in C++
#include <iostream>

using namespace std;
int main ()
{
    cout << "Hello World! ";
    cout << "I'm a C++ program";
    system("pause");
    return 0;
}

This compiles successfully, but when I run it, the terminal screen comes for a second and then vanishes. How can I keep the output screen of the program visible?

Community
  • 1
  • 1
Bandayar
  • 164
  • 1
  • 2
  • 11
  • @Mysticial I m using the solution as James Quoted out there but still not working "At the end of your main function, you can call: std::getchar(); This will get a single character from stdin, thus giving you the "press any key to continue" sort of behavior (if you actually want a "press any key" message, you'll have to print one yourself). You need to #include for getchar. – Bandayar Mar 05 '12 at 04:38
  • Then it looks like there are other problems. Try running it from the command line and see what it says. Maybe it didn't get compiled properly and refuses to run at all. – Mysticial Mar 05 '12 at 04:49
  • No its comipling successfully how to run it from command line – Bandayar Mar 05 '12 at 04:51

2 Answers2

1

You need to do cause the program to stop and wait for input. Use

system("pause");

at the end of your program before the return from main.

Jarryd
  • 1,312
  • 11
  • 17
0

In addition to the options already presented (std::getchar, cin, system("pause"), if the only reason you want the window to persist is to read the output of your program (i.e. debugging), you can simply run the executable from a command prompt.

If you don't mind running the application this way, you can avoid having extra code to prompt a user for input (even if it just a single line) - and if you don't need the window to stay open under normal usage, you don't have to modify anything about your code.

tmpearce
  • 12,523
  • 4
  • 42
  • 60