2010-04-27 285 views
-1

可能重复:
How to stop C++ console application from exiting immediately?如何让屏幕暂停?

这样的IM学习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 */ 
+1

这个问题以前曾问过这里:http://stackoverflow.com/questions/2529617/how-to-stop-c-console-application-from-exiting-immediately(有几种解决方案列在那里)。 – 2010-04-27 23:26:14

回答

5

在Visual C++,您可以:

  • 将断点在main结束括号并运行安装到调试器(调试 - >启动调试)。当断点被击中时,您将能够查看控制台窗口。
  • 从调试器运行分离(Debug - > Start Without Debugging)。当应用程序终止时,控制台窗口将保持打开状态,并显示“按任意键继续...”提示。
+0

我试过“没有调试就开始”,但它仍然关闭我。 .. – Dakota 2010-04-27 23:35:09

+0

@Dakota:确保您的应用程序是一个控制台应用程序(项目属性 - >配置属性 - >链接器 - >系统 - >子系统应该是“控制台(/ SUBSYSTEM:CONSOLE)”)。 – 2010-04-27 23:38:25

+0

它工作!谢谢詹姆斯。 – Dakota 2010-04-27 23:42:38

1

尝试return声明之前加入system("PAUSE");main末。

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

+0

只要暂停在路径中,它就会工作 - 但它会使程序不必要地依赖于Windows。 – redtuna 2010-04-27 23:24:35

+0

是的,但后来我不会得到我的答案坚持下去..我会吗? – Dakota 2010-04-27 23:25:14

+1

这真的会起作用吗?暂停不是可执行文件,它是命令shell的一部分? '系统(“cmd.exe/c pause”);'可能更多是票据,但是它越来越不利于编程实践。 – 2010-04-27 23:27:51

1

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

1

你已经在你的代码的答案..在程序的末尾添加另一CIN,P用户必须按方案,继续并退出

1

我通常使用cin.getchar输入( )等待一个角色。