-1

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

所以我正在学习 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 */
4

5 回答 5

5

在 Visual C++ 中,您可以:

  • 在调试器的右大括号处放置一个断点main并运行附加到调试器(调试-> 开始调试)。当断点被命中时,您将能够查看控制台窗口。
  • 与调试器分离运行(调试 -> 不调试启动)。当应用程序终止时,控制台窗口将保持打开状态,并显示“按任意键继续...”提示。
于 2010-04-27T23:25:54.773 回答
2

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

于 2010-04-27T23:30:56.117 回答
1

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

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

于 2010-04-27T23:21:48.497 回答
1

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

于 2010-04-27T23:21:52.950 回答
1

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

于 2010-04-27T23:27:54.720 回答