Hello World! wont stay open

Hi, I'm very new at this and am atempting to follow this sites tutorial it is helping me learn but the first example th Helo World one wont stay open when i run it. It just opens then instantly closes. I am using Bloodshed Dev-C++ if that helps. any help wil be greatly appreciated.

Thanks
Try adding the following to the line right before return 0:

cin.get();


cin.get() is another function call which reads in input and expects the user to hit the return key.
An alternative way is to press Windows+R, then type in 'cmd'. Then navigate to the directory that the .exe is in, and type in the program's name without the .exe extension. That way, it will run the program and close, but you'll see the output of it.
If yor using a windows use system("pause>nul") same as system("pause>nul") but without a message e.g "Press any key to continue ..."
Thanks guys, Ill try those out now
system("pause >nul") is the best way.
Using system("pause >nul") is NOT the best way. It's not cross-platform, and using system() at all when a C++ solution is available is BAD BAD BAD.

Use cin.get(), it's far better.

Or, just run it in the console, as it's supposed to be done.
y put a nul in it... when you can use system ("pause"); only?
pause >nul will send the output to an imaginary stdout alternative, so you won't see the 'Press any key to continue...' bit. But still, DON'T DO IT.
I have the exact same problem, and i know i can fix it using one of the above options, but I'd like to stick to the book as much as I can, so is there any way i can change the settings so that I don't need to use those solutions?

1
2
3
4
5
6
7
8
9
include <iostream>
using namespace std;

int main()
{
           cout <<"Never fear, C++ is here!"
           return 0;
}
Last edited on
dgo: The proper way to do it would be to run it from the console. When you think about it, you want the program to stop as soon as it's finished doing whatever it's going to be doing. It's not up to the application to give the user time to read it, it's up to the operating system handling the stdout. This is why I miss Linux :(

But if you use Code::Blocks, you can choose 'Build & Run', which will compile your program, run it, and pause for you at the end.
Do you suggest I convert my computer to linux?
No, merely that you press Control+R, type in 'cmd', and then navigate to the directory it's saved in. Then, you can run the program from there. The program will close instantly, but the little black rectangle window (ie. the console window) will not close.

Though, if you want to go ahead and use Linux, feel free. It's made for programmers :).
1
2
3
4
5
6
7
8
9
include <iostream>
using namespace std;

int main()
{
           cout <<"Never fear, C++ is here!"
           system("PAUSE");
           return 0;
}


For any book examples, you just have to add that system("pause") message before your return 0 in main....Or you could use the system("pause>nul"); as well....either will work.
Last edited on
that is why it depends in the IDE that you use in codeblocks you dont have to use a system("PAUSE")..
Topic archived. No new replies allowed.