简体   繁体   English

std::cout 没有 output 任何东西

[英]The std::cout does not output anything

I am working through Stroustrup's book, Programming Principles and Practice Using C++.我正在阅读 Stroustrup 的书《使用 C++ 进行编程原理和实践》。 I am able to enter the temperatures but I do not get std::cout in the console.我可以输入温度,但在控制台中没有得到 std::cout。 I have no compile errors.我没有编译错误。

Here is the code.这是代码。


#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;
}


The problem with your code is that it keeps looping waiting for input.您的代码的问题在于它一直在循环等待输入。 I changed it a little to ask how many values to input in order for you to check that the output is actually working.我稍微改变了一下,询问要输入多少值,以便您检查 output 是否实际工作。

#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;
}

You don't see anything in output because you the program terminates as soon as the instructions to execute finish.您在 output 中看不到任何内容,因为您的程序在执行指令完成后立即终止。 You could use getch();你可以使用 getch(); or system("pause");或系统(“暂停”); In this way the program will require a keyboard command.这样,程序将需要键盘命令。

I tried your code online and actually got the std::cout -prints in the console.我在线尝试了您的代码,实际上在控制台中获得了std::cout -prints。 However, as jrok and Jarod42 mentioned in the comments, the user has to terminate the while-loop by entering input that can't be parsed to a double.但是,正如 jrok 和 Jarod42 在评论中提到的那样,用户必须通过输入无法解析为双精度的输入来终止 while 循环。 Revisit section 4.6.3 A numeric example in Stroustrup's book (Programming Principles and Practice Using C++) again.再次访问 Stroustrup 的书(Programming Principles and Practice Using C++)中的第4.6.3 A numeric example There it says:那里说:

We used the character '|'我们使用了字符“|” to terminate the input — anything that isn't a double can be used.终止输入——任何不是双精度的都可以使用。 In §10.6 we discuss how to terminate input and how to deal with errors in input.在第 10.6 节中,我们讨论了如何终止输入以及如何处理输入中的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM