c++ - 尽管我尝试使用 cin.get() 停止它,但 Visual Studio 命令提示符仍关闭

标签 c++ visual-c++ visual-studio-2012

我的以下基本 C++ 代码无法正常工作。当我在 Visual Studio Express 中运行它时,它只会在我输入第二个数字后关闭命令提示符。提前致谢。

#include <iostream>

using namespace std;

int main()
{

    int num1, num2, answer;

    cout << " Enter a number: ";
    cin >> num1;
    answer = num1 * num1;
    cout << " the sum of the number is: " << answer << endl;

    cout << "Enter a 2nd number";
    cin >> num2;
    answer = answer + num2;
    cout << "The sum of the two numbers is: " << answer << endl;

    cin.get();
    return 0;
}

最佳答案

问题是输入缓冲区中有剩余字符。

无论如何,您不应该添加“棘手的”命令来保持控制台打开(您必须记住将它们从“生产”代码中删除)。

您可以不使用调试器模式(CTRL + F5)运行您的程序,Visual Studio 将保持控制台应用程序窗口打开,直到您按下一个按钮(只需检查 Project -> Properties -> Linker -> System -> Sub System -> Console (/SUBSYSTEM:CONSOLE) 中的设置)。

当然,如果您正在调试 (F5),return 0; 上的断点是最佳选择。

关于c++ - 尽管我尝试使用 cin.get() 停止它,但 Visual Studio 命令提示符仍关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26191061/

相关文章:

c++ - 使用 pion-net c++ 的 HTTP 客户端请求响应

C++ 外部定义内联函数

asp.net - 在.NET中管理多个服务引用的最佳方法

c++ - 为什么我的程序在分配更多线程的情况下执行时间更长?

c++ - 为什么我不能使外部类型看起来像 C++ 中的内部类型?

c++ - 在Qt中链接obj文件

c++警告C4114相同类型的限定符使用了不止一次

c++ - 库正常编译时出现链接器错误 (LNK2001)

c++ - Visual Studio 2012 Express 中 std::vector 的自动矢量化没有发生

c# - 调试时,有没有办法判断一个对象是否是不同的实例?