This question already has answers here:
How to keep the console window open in Visual C++?

(22个答案)


6年前关闭。




我正在尝试使用Visual Studio 2013学习C++,但是存在一个使我无法继续进行的问题。在调试中启动控制台并从用户控制台获取输入后,立即关闭。如何使程序等待命令关闭?
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double number, answer;
    cout << "Enter a number: ";
    cin >> number;
    answer = sqrt(number);
    cout << "Square root is " << answer << endl;
    cin.get();
    return 0;
}

最佳答案

删除声明

cin.get();

并使用Ctrl + F5从IDE运行程序。

这个说法
cin.get();

在上面的输入语句中输入数字后,读取标准流的输入缓冲区中存在的换行符。或者在调用cin.get()清除缓冲区之前,使用带有适当参数的cin.ignore调用(至少作为cin.ignore())。

关于c++ - C++控制台应用程序立即退出? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24743991/

10-12 03:03
查看更多