Microsoft Visual C++ console windows exits immediately

After creating a basic game, I thought I would brush on some advanced OOP. (I only used really basic classes in my game). But when I came back to the console, the window disappeared immediately. I tried using cin.get(); and system("pause"); but none of them worked and I cannot see why. I just created the most basic program ever
1
2
 int x = 5;  cout << x;   return 0;
//cin.get(); || system("pause");  

If you want it to pause add the code to cause the program to pause. Or add a breakpoint on the return 0;

BTW - not a good idea to have all you code on the same line, you won't be able to add a breakpoint on return 0;
My code is not all on the same line, I just typed it out on here
Since you're on Visual C++, a bit old and crude way to "stop" the console window from closing is to use a
getch();
at the end of your program.
You'll have to include conio.h as a header file.

So the above program that you wrote will become:-
1
2
3
4
int x = 5;
cout << x;
getch(); // #include<conio.h>
return 0;

This will stop the program flow, with the program waiting for you to press a key. The program flow will continue when you press a key.
Topic archived. No new replies allowed.