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

孤街醉人 提交于 2019-12-21 17:22:12

问题


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?


回答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.




回答2:


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




回答3:


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.




回答4:


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




回答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;




回答6:


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



来源:https://stackoverflow.com/questions/4164077/c-on-windows-the-console-window-just-flashes-and-disappears-whats-going-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!