The while loop

Hi

I want to input a list of day temperatures and count the number days with temperature above 20 degrees. I want this program display the result number of days correctly. A temperature of -100 indicates the end of the data.

This program is not giving the correct out put .


// calculate the number of days with temp greater than 20 degrees.
#include <iostream>
using namespace std;

int main ()
{
int nrAbove ;
int temp ;
nrAbove = 0;
temp = 0;
cout <<"Temperature of first day (-100 for end of input) : ";
cin >> temp;
while (temp > -100)

{
cout <<"Temperature of next day (-100 for end of input) : ";
cin >> temp;
if (temp > 20)
temp += nrAbove;
nrAbove++;
}
cout << "Number of days with temperature above 20 degrees C is ";
cout << nrAbove << endl;

return 0;
}
Last edited on
I noticed that you have an infinite loop in the middle of your program. It doesn't give the user the opportunity to enter more data, so unless the user enters -100 the first time, the program will continue running indefinitely. Ask the user for another input within that loop, like so:
1
2
3
4
5
6
7
while (temp > -100)
{
    if (temp > 20)
    nrAbove++;
    cout << "Temperature of next day:" << endl;
    cin >> temp;
}
thanx roflzombie

you the star, i think the program is not giving a correct output there must be a logical error in it.

The program suppose to count all the temp that is greater than 20 degrees
Last edited on
Topic archived. No new replies allowed.