tags:

views:

164

answers:

7

I'm using Visual C++ 2010 Express and I just started learning C++.

So when I want to run this code:

#include <iostream>
using namespace std;

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

It works, but the program exits immediately after I started it, how should I keep the program alive?

+1  A: 
cout<<"Please press any key to quit";
char number;
cin>>number;
Csaryus
You were right. I corrected.
Csaryus
`press any key AND RETURN to quit`
smerlin
+5  A: 

If it's just to read the output, you don't need the program to stay "alive", just run it from a command prompt window and the output will remain visible. You can also use a debugger to break execution at a particular point.

There are plenty of ways, good and bad, to do it with code:

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello World! ";
    cin.get(); // Wait for some input, as suggested by PigBen
    return 0;
}

or:

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello World! ";
    Sleep(1000); // one second
    return 0;
}

or, even though this is a Bad Idea:

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello World! ";
    while (true) { }
    return 0;
}

What are you trying to accomplish?

Edited to note that infinite loops are bad, even though they'll technically keep the program alive forever.

peachykeen
Does istream have a () operator?
PigBen
@PigBen: not sure, but on second thought, not seeing one. Tweaked it a bit to be more correctish.
peachykeen
Correct. But why don't you just use cin.get() instead of creating a dummy variable?
PigBen
Good idea. Edited, again.
peachykeen
-1: Although the 3 solutions "keep the program alive", the user just wants to prevent the console from disappearing in order to see the output. Your 1st solution is not standard (no guarantee it blocks, see my comment to "bjoernz" answer). Your 2nd solution is definately non portable and a header file is missing. Your 3rd solution is definitely not something I would recommend considering it will hog the CPU.
André Caron
@André: I thought I covered that in the note before the code?
peachykeen
@peachykeen: and after. I just wanted to point out that all 3 solutions have drawbacks, and that the real answer lies is Tim Rupe's although this one was voted higher.
André Caron
+2  A: 

Someone who is familiar with windows console app developing will be able to help you better, until then try this:

#include <iostream>

int main(){
    std::cout << "Hello World! ";
    std::cin.get(); // waits for input, press enter to continue
    return 0;
}

Reference for std::cin.get()

bjoernz
-1: The documentation says "Extracts a character from the stream and returns its value (casted to an integer).". There is no requirement to block if no characters are available, so although it works in the poster's configuration, it is (subtly) non standard. I've had problems with this on Linux systems, where it doesn't block!
André Caron
@André Thanks for pointing that out, I just tried it on Mac OS X (no issues). I have never tried it on Linux.
bjoernz
+1  A: 

First off, you probably want to add a newline output to flush this to the console.

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

If you really don't want to exit right away, you could wait for console input using cin after you write this out, or call Sleep(10000) for a 10 second delay and so on.

Steve Townsend
+6  A: 

In Visual studio you have two options to run a program. There is absolutely no need to modify your code as many other posts are suggesting.

1) Run with debugging. You are probably using this, and to make it stop anywhere, you need to set a breakpoint.

2) Run without debugging. This should leave the console window open, and prompt you to press a key before the window closes.

Tim Rupe
+1 for mind-reading and pointing out the *real* cause. The poster is confused between keeping the program alive and the console window disappearing.
André Caron
+4  A: 
system("Pause");

"Press any key to continue..."

pinkfloydx33
This is the best answer I see. I can't believe that an infinite loop was voted up...
Ed Swangren
@Ed: If you're referring to my answer, which has the only infinite loop I can see, I don't expect people to use it. Just grabbed a few ways off the top of my head. I should probably re-order them.
peachykeen
I'm surprised so many people are suggesting code modification when it isn't needed.
Tim Rupe
I have never seen "system("Pause");" before. Google provides this link with a lot of reasons against this: http://www.gidnetwork.com/b-61.html
bjoernz
+1  A: 

Set breakpoint at the end of main function.

Svisstack
That would assume you were running in debug mode always, wouldn't it?
pinkfloydx33