为什么第二个“cout”线不会出现在控制台中;以下教程

时间:2014-12-30 05:43:50

标签: c++ cout

http://www.learncpp.com/ 关注上面的网站...

#include "stdafx.h"
#include <iostream>

int main()
{
    using namespace std;
    cout << "Enter a number: ";
    int x = 0;
    cin >> x;
    cout << "You entered " << x << endl;
    return 0;
}

除了一件事,一切都很好。我根本没有得到第二个cout显示器。我通过互联网尝试了各种建议。但是,说实话,我更好奇为什么它不适合我。 使用VS express 2013

我肯定会找到很多方法让它实现一些挖掘。但是,我想知道为什么在这种情况下,它根本无法正常工作。代码看似准确,软件就像没有错误一样。那么,第二次cout显示在哪里?

3 个答案:

答案 0 :(得分:1)

它的打印,这里唯一的问题是你的代码中没有任何东西阻止你的代码退出。最后使用另一个cin并查看它是否有效。或者只是从命令行运行程序。

#include <iostream>

int main()
{
    using namespace std;
    cout << "Enter a number: "; 
    //Enter a number say 5 after you see this line on screen and press ENTER
    int x = 0;
    cin >> x;
    cout << "You entered " << x << endl;
    cin.get(); //just a dummy cin to halt screen to see output
    return 0;
}

答案 1 :(得分:0)

要使代码超过cin >> x行,您需要输入一个数字并按 Enter

如果您已经这样做,并且第二个cout输出没有出现,请确保您已从头开始重建代码并且您正在运行完全您问题中显示的代码。

如果这些都没有区别,那么问题可能在于您的控制台而不是您的代码。

答案 2 :(得分:0)

因为您没有暂停窗口以查看最后一行,所以在第二个cout之后添加此行:

system("pause");