Hello World! wont stay open

Jan 21, 2008 at 7:39am UTC
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
Jan 21, 2008 at 11:07am UTC
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.
Jan 21, 2008 at 1:35pm UTC
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.
Jan 21, 2008 at 1:37pm UTC
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 ..."
Jan 22, 2008 at 12:09am UTC
Thanks guys, Ill try those out now
Jan 22, 2008 at 6:50am UTC
system("pause >nul") is the best way.
Jan 22, 2008 at 1:33pm UTC
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.
Jan 22, 2008 at 2:12pm UTC
y put a nul in it... when you can use system ("pause"); only?
Jan 22, 2008 at 4:57pm UTC
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.
Jan 22, 2008 at 7:42pm UTC
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 Jan 22, 2008 at 7:44pm UTC
Jan 22, 2008 at 7:48pm UTC
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.
Jan 22, 2008 at 7:51pm UTC
Do you suggest I convert my computer to linux?
Jan 22, 2008 at 7:59pm UTC
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 :).
Jan 23, 2008 at 5:29am UTC
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 Jan 23, 2008 at 5:31am UTC
Jan 24, 2008 at 3:34am UTC
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.

AltStyle によって変換されたページ (->オリジナル) /