无法读取程序输出 [英] Can't read program output

查看:94
本文介绍了无法读取程序输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在学习C ++,我遇到了一个小问题,我似乎无法解决。当我写一个程序以从用户输入信息时,程序在用户输入信息之后立即关闭,并且不显示输入的信息。

So I'm learning C++ today and I've come across a little problem that I can't seem to solve. When I write a program to input information from a user, the program closes right after the user inputs the information and does not display the inputed information.

例如,以下是代码:

#include <iostream>
#include <string>

using namespace std;

int main() {
    //Declar vairables
    char letter = 'A';
    int integer = 0;
    float dec = 0.0f;

    cout << "Enter a letter: ";
    cin >> letter;

    cout << "Enter an integer: ";
    cin >> integer;

    cout << "Enter a float number: ";
    cin >> dec;

    cout << endl;

    //Output what user entered
    cout << "letter: " << letter << endl;
    cout << "integer: " << integer << endl;
    cout << "float number: " << dec << endl;
}

现在当我运行这个,控制台要求:

Now when I run this, the console asks me to:


  1. 输入字母

  2. 输入整数

  3. 输入浮点数

然后,程序立即关闭,不显示该段代码应该执行的输出:

After this though, the program instantly closes without displaying the output that this segment of code is supposed to do:

//Output what user entered
cout << "letter: " << letter << endl;
cout << "integer: " << integer << endl;
cout << "float number: " << dec << endl; }

这让我抓头了,因为没有编译错误,的eInstitute发布的 C ++编程游戏

This has me scratching my head as there are no compiling errors, and the code is ripped straight out of C++ Programming for Games by eInstitute Publishing.

推荐答案

您的程序正在退出。由于命令行通过执行您的进程打开,当进程退出时它关闭。您可以在Main函数结束时添加对 cin 的调用,以暂停直到按下enter,例如:

Your program is exiting. As the command line was opened via the execution of your process, it closes when your process exits. You can add a call to cin at the end of the Main function in order to pause until enter is pressed, something like:

cout << "Press any key to close the program";
char c;
cin >> c;

这篇关于无法读取程序输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆