C++ on Windows - the console window just flashes and disappears. What's going on?

87,971

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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.

Solution 5

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;

Share:
87,971
John Smith
Author by

John Smith

Updated on July 09, 2022

Comments

  • John Smith
    John Smith almost 2 years

    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?

    • Simone
      Simone over 13 years
      Did you try to run the executable from the command line?
    • thecoshman
      thecoshman over 13 years
      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
  • Simone
    Simone over 13 years
    cin.get() is a better approach in C++ programs.
  • sbi
    sbi over 13 years
    I dislike changing the program to do this.
  • Simone
    Simone over 13 years
    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.
  • sbi
    sbi over 13 years
    Or put a breakpoint at the closing brace of main().
  • Chubsdad
    Chubsdad over 13 years
    @sbi: wonder what you will say to this.
  • Stuart Golodetz
    Stuart Golodetz over 13 years
    Don't think you mean cin << i -- perhaps cin >> i instead?
  • John Smith
    John Smith over 13 years
    Ctrl+F5 doesn't work... see my edit please
  • Bear
    Bear about 8 years
    I would accept your answer!! worked perfectly for me.