C++ 如何让屏幕暂停?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2725823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:30:30  来源:igfitidea点击:

How do i get the screen to pause?

c++visual-c++visual-c++-2010

提问by Dakota

Possible Duplicate:
How to stop C++ console application from exiting immediately?

可能的重复:
如何阻止 C++ 控制台应用程序立即退出?

So im learning c++ and i was given this example and i wanted to run it. But i cannot get it to stay up, unless i change it. How do i get Microsoft visual 2010 to keep up the screen when it gets to the end of the program after I release it?

所以我正在学习 C++,我得到了这个例子,我想运行它。但我不能让它熬夜,除非我改变它。我如何让 Microsoft Visual 2010 在我发布程序结束时保持屏幕显示?

#include<iostream>
using namespace std;

int area(int length, int width);        /* function declaration */

/* MAIN PROGRAM: */
int main()
{
    int this_length, this_width;      

    cout << "Enter the length: ";             /* <--- line 9 */
    cin >> this_length;
    cout << "Enter the width: ";
    cin >> this_width;
    cout << "\n";                             /* <--- line 13 */

    cout << "The area of a " << this_length << "x" << this_width;
    cout << " rectangle is " << area(this_length, this_width);

    return 0;
}
/* END OF MAIN PROGRAM */

/* FUNCTION TO CALCULATE AREA: */
int area(int length, int width)   /* start of function definition */
{
    int number;

    number = length * width;

    return number;
}                                 /* end of function definition */
/* END OF FUNCTION */

回答by James McNellis

In Visual C++ you can either:

在 Visual C++ 中,您可以:

  • Put a breakpoint at the closing brace of mainand run attached to the debugger (Debug -> Start Debugging). When the breakpoint is hit you will be able to view the console window.
  • Run detached from the debugger (Debug -> Start Without Debugging). When the application terminates, the console window will stay open with a "Press any key to continue..." prompt.
  • 在右大括号处放置一个断点main并运行附加到调试器(调试 -> 开始调试)。当断点被击中时,您将能够查看控制台窗口。
  • 从调试器分离运行(调试 -> 不调试启动)。当应用程序终止时,控制台窗口将保持打开状态,并显示“按任意键继续...”提示。

回答by Michael Dorgan

I usually use cin.getchar() to wait for a character.

我通常使用 cin.getchar() 来等待一个字符。

回答by Jonas B

You already have the answer in your code.. add another cin at the end of your program ;P user would have to press enter for program to continue and exit

您的代码中已经有了答案.. 在程序末尾添加另一个 cin ;P 用户必须按 Enter 才能让程序继续并退出

回答by Nathan Osman

Try adding system("PAUSE");to the end of mainbefore the returnstatement.

尝试添加system("PAUSE");到语句main之前的末尾return

This executes the PAUSE system command which waits for a key to be pressed.

这将执行等待按键被按下的 PAUSE 系统命令。

回答by redtuna

The easiest way is to wait for the user to press a key before returning from main.

最简单的方法是在从 main 返回之前等待用户按下某个键。