我正在做一个用于培训的控制台应用程序。 当我调试下面的代码时,它会在显示
之前自行关闭 std::cout << e << std::endl;
如果我将整数 e 及其输出移到 std::cout << "Enter a number:";
之前它工作正常,但是当像下面这样列出时,只有它的输入有效,并且控制台会自行关闭。
#include "stdafx.h"
#include <iostream>
int raisemath()
{
std::cout << "Enter an integer: "; // ask user for an integer
int a; // allocate a variable to hold the user input
std::cin >> a; // get user input from console and store in variable a
return a; // return this value to the function's caller (main)
}
int main()
{
int number;
std::cout << "Enter a number:";
std::cin >> number;
std::cout << "You entered " << number << std::endl;
int e = raisemath();
std::cout << e << std::endl;
return 0;
}
我想知道为什么?
最佳答案
在最后一个 std::cout
之后,没有什么可以阻止控制台关闭。
当您更改 std::cout
的位置时,它后面会有一个输入语句。因此控制台等待输入以继续执行。
通过在最后一个 return 语句处添加断点或在 return 语句之前输入空白可以轻松实现防止控制台关闭。
您可能还想检查调试器设置,看看是否选中了“调试停止时自动关闭控制台”。 (在工具>选项>调试中)
关于c++ - 应用程序在显示最终输出之前结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54122554/