Problems with cout dissapearing instantly

When I run a program with this code:
1
2
3
4
5
6
7
8
#include <iostream>

using namespace std;

int main(){
	cout << "Hello World!";
}


the window with cout dissapears instantly. I have looked a ton about this problem and the common answer is to press ctrl-F5 or run without debugging. I have tried this and all I get is a red checkmark in the selected line. Even worse, F5 is my browser key so if I miss ctrl I start up the browser. I have been looking for other keys to do this, and the only thing I've found is the shortcut configurer, but it doesn't have the right configurations. The only thing that helps, is adding
int choose;
and
cin >> choose;.
I know what it means.
I don't want to add that to every program, so I want to find another way.
I want to get that to work ASAP.
thanks
See the locked thread at the top of this forum "Console closing down"
Don't fall the system() trap!
Yes, I looked at the forum post and it was very helpful. I won't use any system() code.
thanks
It's just a no no in production code etc, it's ok if you're just dinking around.
#include <iostream>

#include<conio.h>

using namespace std;

int main(){
cout << "Hello World!";
getch();
return 0;
}
Create a batch file with the following content:

a.exe
pause

Then start the batch file.
venkyHyd's answer will fix it. use conio.h header and getch()
Where do you people come from?

A perfectly valid answer was given, and then you all suggest the use of a non-standard header to solve the problem?

conio.h is nonstandard, please do not recommend it:
http://en.wikipedia.org/wiki/Conio.h
LB I am a beginner too. Can you give the link that you referred in the first post please? thanks mate!
It's the second post from the top of this forum, outlined in bold black, called "Console Closing Down". If you can't find it, then you shouldn't be using the internet ;)
My bad sorry.
So the better answer is to use the limits header?
The best answer is the first reply by Duoas ;) so, yes.
Thanks I will read more on that
TY 'el bee' :)
Run your program from the console?

(sorry, had to say that once)
Last edited on
You have to use some statement to simulate a pause -if you don't want to run it from the console- Windows .-.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

//You can use "using namespace std;" instead of..

int main(){

//print output + new line
cout<<"Hello World!"<<endl; 

cin.ignore(); //pause

return 0; //indicate successful termination
}//end main
Or you can just read the sticky topic at the top of this forum which explains the proper way to do it correctly ;)
Topic archived. No new replies allowed.