应用程序在显示最终输出之前结束

Application ends before displaying final output

本文关键字:结束 输出 显示 应用程序      更新时间:2023-10-16

我正在做一个用于培训的控制台应用程序。当我调试下面的代码时,它会在显示之前关闭自己

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的位置时,后面会有一条input语句。因此控制台会等待输入继续执行。

通过在最后一条返回语句处添加断点或在返回语句之前添加空白输入,可以很容易地防止控制台关闭。

您可能还想检查调试器设置,看看是否选中了"调试停止时自动关闭控制台"。(位于"工具">"选项">"调试"中)