darkdata
darkdata

Reputation: 21

The std::cout does not output anything

I am working through Stroustrup's book, Programming Principles and Practice Using C++. I am able to enter the temperatures but I do not get std::cout in the console. 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;
}


Upvotes: 2

Views: 519

Answers (3)

mrcl
mrcl

Reputation: 21

I tried your code online and actually got the std::cout-prints in the console. 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. Revisit section 4.6.3 A numeric example in Stroustrup's book (Programming Principles and Practice Using C++) again. 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.

Upvotes: 0

sergiom
sergiom

Reputation: 4877

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.

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

Upvotes: 1

Valerio Balzano
Valerio Balzano

Reputation: 11

You don't see anything in output because you the program terminates as soon as the instructions to execute finish. You could use getch(); or system("pause"); In this way the program will require a keyboard command.

Upvotes: 0

Related Questions