【问题标题】:The std::cout does not output anythingstd::cout 不输出任何内容
【发布时间】:2020-11-26 12:50:46
【问题描述】:

我正在阅读 Stroustrup 的书,Programming Principles and Practice Using C++。我可以输入温度,但在控制台中没有得到 std::cout。我没有编译错误。

这里是代码。


#include <iostream>
#include <vector> // I added this which is different from the book

void push_back(std::vector<double> temps, double value) { // I added this which is different from the book, maybe I don't need this but I found it doing a search
    temps.push_back(value);
}

int main() {
    std::vector<double> temps;

    double temp = 0;
    double sum = 0;
    double high_temp = 0;
    double low_temp = 0;

    std::cout << "Please enter some integers"<< '\n';  // I added this in for the prompt

    while (std::cin >> temp)
        temps.push_back(temp);

    for (int i = 0; i < temps.size(); ++i) {
        if (temps[i] > high_temp) high_temp = temps[i];
        if (temps[i] < low_temp) low_temp = temps[i];
        sum += temps[i];
    }
        std::cout << "High temperature: " << high_temp << std::endl;  // There is no output for this and the next two lines
        std::cout << "Low temperature: " << low_temp << std::endl;
        std::cout << "Average temperature: " << sum/temps.size() << std::endl;

    return 0;
}


【问题讨论】:

  • 什么控制台?您如何编译、运行和查看该程序的输出?
  • 您几乎可以肯定是在您的 IDE(开发环境)中运行该程序。这将创建一个窗口,并且程序的输出将定向到该窗口。然后,当程序终止时,窗口被破坏并以足够快的速度消失,以至于您看不到输出。要么找到一种方法在不会立即销毁的窗口中运行程序,要么在 main() 的末尾添加一条等待额外输入的语句。
  • void push_back(std::vector&lt;double&gt; temps, double value) 您的向量是按值传递的,因此在函数中对其所做的任何更改在函数外部都不可见,因为该对象在函数结束时消失。请改用void push_back(std::vector&lt;double&gt;&amp; temps, double value)。由于您似乎没有使用该功能,所以现在没关系,但如果您将来使用,您应该注意它。
  • 还有一件事——像while(std::cin &gt;&gt; temp) 这样的循环会一直循环,直到你输入一些不能是有效双精度的内容(例如,一个字母或一个 EOF 字符(Windows 上的 ctrl+z ))。仅在新行上按 Enter 是不够的。
  • 如何传递参数?在交互模式下,std::cin &gt;&gt; temp 会一直阻塞,直到您明确停止输入(某些终端为 Ctrl+D)或关闭终端。

标签: c++


【解决方案1】:

您的代码的问题在于它一直在循环等待输入。 我稍微修改了一下,询问要输入多少个值,以便您检查输出是否实际工作。

#include <iostream>
#include <vector> // I added this which is different from the book

void push_back(std::vector<double> temps, double value) { // I added this which is different from the book, maybe I don't need this but I found it doing a search
    temps.push_back(value);
}

int main() {
    std::vector<double> temps;

    double temp = 0;
    double sum = 0;
    double high_temp = 0;
    double low_temp = 0;
    int ntemp;

    std::cout << "Enter the number of temperatures to input:";
    std::cin >> ntemp;

    std::cout << "Please enter " << ntemp << " doubles"<< '\n';  

    for (int i = 0; i < ntemp; ++i)
    { 
        std::cin >> temp;
        temps.push_back(temp);
    }

    for (int i = 0; i < temps.size(); ++i) {
        if (temps[i] > high_temp) high_temp = temps[i];
        if (temps[i] < low_temp) low_temp = temps[i];
        sum += temps[i];
    }
        std::cout << "High temperature: " << high_temp << std::endl;  // There is no output for this and the next two lines
        std::cout << "Low temperature: " << low_temp << std::endl;
        std::cout << "Average temperature: " << sum/temps.size() << std::endl;

    return 0;
}

【讨论】:

  • 现在这是指导输入数量的明智之举。很酷。
  • 您是否以这种方式看到控制台中的输出?如果是这样,问题不在于 std::cout 语句;)
【解决方案2】:

您在输出中看不到任何内容,因为您的程序会在执行指令完成后立即终止。 你可以使用 getch();或系统(“暂停”); 这样程序将需要一个键盘命令。

【讨论】:

  • 我也会在以后的节目中尝试一下。
【解决方案3】:

I tried your code online 并且实际上在控制台中得到了std::cout-prints。但是,正如 cmets 中提到的 jrok 和 Jarod42 一样,用户必须通过输入无法解析为双精度的输入来终止 while 循环。再次访问 Stroustrup 的书(Programming Principles and Practice Using C++)中的4.6.3 A numeric example 部分。上面写着:

我们使用了字符“|”终止输入——任何东西 那不是可以使用双精度的。在第 10.6 节中,我们讨论了如何终止输入和 如何处理输入错误。

【讨论】:

    猜你喜欢
    • 2017-04-25
    • 1970-01-01
    • 2013-09-11
    • 2011-07-08
    • 2015-02-03
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多