简体   繁体   English

C ++初学者,执行窗口迅速消失

[英]C++ beginner, execution window disappears quickly

I am a beginner in C++ and I was trying to write a program that finds the average of two numbers, but when I run the program, the window disappears without allowing me to see the result. 我是C ++的初学者,我试图编写一个找到两个数字平均值的程序,但是当我运行该程序时,窗口消失了,而我却没有看到结果。 Can someone please help me? 有人可以帮帮我吗? Thanks 谢谢

 #include <iostream>
 using  namespace std;
 int main()
 {
 int number1,number2,answer;
 cout << "number1? ";
 cin >> number1;
 cout << "number2? ";
 cin >> number2;

 answer = (number1 + number2)/2;

 cout << answer<<endl;
 return 0;
 }

Solution #0 (proper): 解决方案0(正确):
Run program from shell ( cmd.exe , bash ) 从shell( cmd.exebash )运行程序

Solution #1 (proper): 解决方案1(正确):
Run program from orthodox file manager. 从正统文件管理器运行程序。 Far Manager or midnight commander. 远东经理或午夜指挥官。

Solution #2 (alternative); 解决方案2(替代);
Redirect output to file. 将输出重定向到文件。 program.exe >file.txt from command line. 从命令行访问program.exe >file.txt

Solution #3 (improper): 解决方案3(不合适):
Launch message box, use "sleep"/"Sleep" to delay program termination. 启动消息框,使用“睡眠” /“睡眠”来延迟程序终止。

Solution #4 (improper): 解决方案4(不当):
Request user input at the end of program. 在程序结束时请求用户输入。

Solution #5 (improper): 解决方案5(不当):
Set breakpoint on "return 0", debug the program. 在“返回0”上设置断点,调试程序。

Solution #6 (windows+msvc): 解决方案#6(Windows + MSVC):
Launch program from msvc by Ctrl+F5 using debug build. 使用调试版本,通过Ctrl + F5从msvc启动程序。 You'll get "press key to continue" prompt. 您将收到“按键继续”提示。

Put a breakpoint at your return statement. return语句中放置一个断点。 It won't stop on an uncaught exception but that can be fixed with a try / catch block at the outermost part of main . 它不会在未捕获的异常上停止,但是可以用main的最外层的try / catch块来解决。

Solution #6 from SigTerm (above) is not guaranteed to work. SigTerm(上面)的解决方案#6不能保证正常工作。 In Visual Studio you should right-click your project and choose: 在Visual Studio中,您应该右键单击您的项目,然后选择:

Properties | 属性| Configuration Properties | 配置属性| Linker | 链接器| System | 系统| SubSystem 子系统

and in the dropdown choose Console (/SUBSYSTEM:CONSOLE) . 并在下拉菜单中选择Console(/ SUBSYSTEM:CONSOLE)

Include this header: 包括以下标题:

#include <stdio.h>

And add a call to getchar() before you return : 并在return之前添加对getchar()的调用:

cout << answer<<endl;

getchar(); // wait for user input

return 0;

Before you return add system("PAUSE"); 返回之前,请添加系统(“ PAUSE”); and this should fix your problem. 这应该可以解决您的问题。

If you are using Visual Studio, hit CTRL+F5 to run. 如果您使用的是Visual Studio,请按CTRL + F5来运行。 That inserts a "Hit RETURN to continue" for a console application. 这将为控制台应用程序插入“点击返回以继续”。

I always hated that...I always find the easiest to be system("pause"); 我一直讨厌……我总是觉得最容易成为system("pause"); right before you return, but that's not a portable solution (cin.get is though). 就在您回来之前,但这不是可移植的解决方案(尽管是cin.get)。 There are many other ways as well, some of which are mentioned in the link below. 还有很多其他方法,下面的链接中提到了其中一些。

http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/1555ce45-8313-4669-a31e-b95b5d28c787 http://social.msdn.microsoft.com/Forums/zh-CN/Vsexpressvc/thread/1555ce45-8313-4669-a31e-b95b5d28c787

Edit: system("pause"); 编辑: system("pause"); is terrible, should never be used, and may or may not end life on this planet as we know it, even if by a beginner in a project called 'Hello'. 是可怕的,绝不应该使用,并且即使我们初学者在一个名为“ Hello”的项目中也可能会终结我们所知的星球上的生命。 Using system("pause") is expensive and dangerous, and if you use it, you'll never see anyone you love again. 使用system("pause")既昂贵又危险,如果使用它,您将再也看不到自己爱的人了。 Don't do it. 不要这样 Use cin.get or something else. 使用cin.get或其他。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM