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