6

Possible Duplicate:
Visual Studio Console App - Prevent window from closing.

I'm starting to learn C++ on Windows and I'm trying a few different development environments: 1. Netbeans with Cygwin compiler 2. MS Visual Studio 2010

For either of them, when I write a very simple Hello World program, I build it and it's fine. But when I try to run the program, the command prompt window pops up really quick and then disappears right away.

This happens whether it's in Debug or Release config. Please help with this - I can't see my program output! :(

Thanks.

EDIT1: Thanks for the replies. This is my code:

#include <iostream>

int main()
{
    std::cout << "This is a test." << std::endl;
    return 0;
}

I tried Ctrl+F5 for "Start without Debugging" and that doesn't work. It still flashes the black console screen then disappears right away.

I also tried adding in std::cin.get(); and this works with Ctrl+F5, but isn't that a really... inelegant workaround solution? I'd rather have my program in the final form.

The breakpoint works, but then I have to run with debugging and the console window flashes and disappears, but then it stays in the background. Any way to get the console to stay in the foreground so I can see program output right away? Seems like that's how it should work.

Any more ideas? Why wouldn't Ctrl+F5 work?

Community
  • 1
  • 1
John Smith
  • 61
  • 1
  • 1
  • 3
  • Did you try to run the executable from the command line? – Simone Nov 12 '10 at 11:23
  • It is doing what you told it to do, start the program, write to the screen, then exit. You basically need to get the application to wait for some user input, like the answers below say – thecoshman Nov 12 '10 at 11:28

6 Answers6

15

After you are done with your program, press Ctrl + F5 ( Run without debugging). This will prompt before closing the window and this is what you want.

user225312
  • 108,033
  • 64
  • 161
  • 179
6

Write cin.get() at the end of the program.

Simone
  • 10,912
  • 1
  • 26
  • 39
  • 4
    I dislike changing the program to do this. – sbi Nov 12 '10 at 11:27
  • 2
    Yes, the correct solution would be to run the application from the command line, IMHO. But since he's a beginners, this makes things work. – Simone Nov 12 '10 at 11:28
2

use Ctrl+F5 to run your program or set a break point in the last line or write cin>> to any vraiable at the end....etc

Mohamad Alhamoud
  • 4,821
  • 8
  • 29
  • 44
1

I think your program just prints Hello World and then exits. That's the reason the console closes immediately. You can run the executable from Command Prompt (Start Menu > Run and type cmd.exe).
Otherwise, you can put std::cin.get() in your code so that program waits for user's input and hence the console window remains open until a key is pressed.

swatkat
  • 4,367
  • 1
  • 21
  • 17
1

Your application is probably working. Make the last command in your console application wait for user input: e.g int i; string i; cout<<"Hello"; cin<<i;

CoderSivu
  • 123
  • 2
  • 7
0

Issue a getchar() before returning or run from cmd.exe

soulseekah
  • 7,890
  • 3
  • 47
  • 54