# Quick newbie question on C++

This topic is 4375 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey there, So I just got a book to start learning some C++ as a hobby type of deal. Anyway, when creating the "Hello World" program, when it runs, it just dissapears. I am using Dev-C++ as the compiler. Heres my code:
#include <iostream>
using namespace std;

int main()
{
cout << "Hello World\n";
return 0;

}

I think I remember something I can add that will make it stay until I hit return or something, but I can't remember what that was. Any help would be appreciated. Thanks!

##### Share on other sites

char c;

cin >> c;

before the return 0; statement.

Dave

##### Share on other sites
Great, thanks Dave!

##### Share on other sites
Anything that can keep a loop or a pause until some kind of time limit or return value from something or input, etc will keep it running. What Dave said is an input. The reason it closes is the program terminates once the main function returns which happens right after it outputs the text, which only takes a few ms. You just need to give the program something to do until you wanna close it.

##### Share on other sites
Another possibility is system("PAUSE"); if you are compiling on windows. This is a system call though so that means it depends on the operating system and some don't have the same command. Let it be known that system("PAUSE"); should be avoided in more serious projects and in place you should use another system that doesn't make a system call. It works well for a quick method of stopping before the end.

##### Share on other sites
Also I think the best is "Sleep"
and then right before the return 0 put
Sleep (5000);
each (1000) delays closing for 1 second

##### Share on other sites
Well i'm surpised no one has said cin.get(); yet. Just add that before return and it waits for you to press enter.

##### Share on other sites
And possibly try:

cout << "Hello World!" << endl;

Going C++, go all the way. Not that your way is wrong, mind. Also, you don't need to return 0, because the C++ spec says that that's the default return for main (and only main - the special function).

Keep coding!

##### Share on other sites
I think it's good practice to return 0, it might be better for him in learning because it shouts out every time he writes a program so it gets stuck in his head. Just like he forgot how to keep the program from closing, he may forget that it automatically return's 0 tomorrow (not that he really needs to worry about what to return right now lol) and not no what's returning without anything there. My opinion though, I just like to have it explicit so you can think a bit less about the little things when reviewing code in depth.

##### Share on other sites
Thanks for all the responses! You'll learn way more than you bargain for when reading and posting here. :)

I didn't know about the return 0; deal. So that's only defaulted in main(), eh? I'll take Ganoosh's advice and go ahead and put that in every time anyway, just to make it stick in my brain.. or something.

I think the cin.get() is what I was thinking of before, just couldn't remember it.

Oh yeah, I have a quick question for you guys. After I'm done with this book, I was thinking about getting the Beginning C++ Game Programming by Michael Dawson. Has anyone had any experience with this book? I read the reviews over in the books section of the site, and it seems to be quite good from what they say over there.

Anyway, thanks again for the helpful responses! I'll plug away. :)