C++在 Windows 上 - 控制台窗口只是闪烁然后消失。发生了什么事?
我'我开始在 Windows 上学习 C++,并且正在尝试一些不同的开发环境: 1.带有Cygwin编译器的Netbeans 2. MS Visual Studio 2010
对于其中任何一个,当我编写一个非常简单的 Hello World 程序时,我构建它并且没问题。但是当我尝试运行该程序时,命令提示符窗口很快弹出,然后立即消失。
无论是在调试配置还是发布配置中都会发生这种情况。请帮忙解决这个问题 - 我看不到我的程序输出! :(
谢谢。
编辑1: 感谢您的回复。这是我的代码:
#include <iostream>
int main()
{
std::cout << "This is a test." << std::endl;
return 0;
}
我尝试使用 Ctrl+F5 来“启动而不调试”,但这不起作用。它仍然闪烁黑色控制台屏幕,然后立即消失。
我还尝试添加 std::cin.get();这适用于 Ctrl+F5,但这不是一个真正...不优雅的解决方案吗?我宁愿我的程序是最终形式。
断点有效,但随后我必须运行调试,控制台窗口闪烁并消失,但随后它停留在后台。有什么方法可以让控制台留在前台,以便我可以立即看到程序输出?似乎这就是它应该如何工作。
还有更多想法吗?为什么 Ctrl+F5 不起作用?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
完成程序后,按
Ctrl + F5
(运行而不调试
)。这将在关闭窗口之前进行提示,这就是您想要的。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.在程序末尾写入cin.get()。
Write cin.get() at the end of the program.
使用
Ctrl+F5
运行程序或在最后一行设置断点或将cin>>
写入末尾的任何vraiable....等use
Ctrl+F5
to run your program or set a break point in the last line or writecin>>
to any vraiable at the end....etc我认为你的程序只是打印
Hello World
然后退出。这就是控制台立即关闭的原因。您可以从命令提示符运行可执行文件(“开始”菜单 >“运行”并键入 cmd.exe)。否则,您可以将
std::cin.get()
放入代码中,以便程序等待用户的输入,因此控制台窗口保持打开状态,直到按下某个键。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.您的应用程序可能正在运行。使控制台应用程序中的最后一个命令等待用户输入:例如 int i;
<代码>
字符串我;
cout<<"你好";
cin<<i;
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;
在从
cmd.exe
返回或运行之前发出getchar()
Issue a
getchar()
before returning or run fromcmd.exe