I wants to input a list of day temperatures and count the number of days with
temperatures above 20 degrees. The C++ program below is supposed to do this and display the result. A temperature of -100 indicates the end of the data.

#include <iostream>
using namespace std;
int main( )
{
float temp;
int nrAbove;
// initialize
nrAbove = 0;
cout << "Temperature of first day (-100 for end of input): ";
cin >> temp;
30
// loop
while (temp > -100)
{
if (temp > 20) // temperature above 20 degrees?
nrAbove++;
} // end of while loop
// results
cout << "Number of days with temperature above 20 degrees C is ";
cout << nrAbove << endl;
return 0;
}


Thanks for taking time to look at this.

Please tell us what kind of problem you need help with. We can't offer anything more than general advice if you don't specify what's wrong with your code.

I guess it will always show 0 or 1, because you can only enter 1 input. If you want to get more inputs, you should put this: cin >> temp; inside your while loop.

These are the error’s I am experiencing once I compile.

[Warning] In function `int main()':

expected `;' before "while"

Maybe because you have "30" in your code?

If you don't need it get rid of it, else put a ; on end of 30

I guess it will always show 0 or 1, because you can only enter 1 input. If you want to get more inputs, you should put this: cin >> temp; inside your while loop.

i added the following code : cin >> temp; inside your while loop.
And i am getting these errors once I compile.

[Warning] In function `int main()':

[Warning] In function `int main()':

Thanks for taking the time to look at my problem.

// How many days with temperature above 20 degrees C?

#include <iostream>

using namespace std;

int main( )
{
float temp;
int nrAbove;
// initialize
nrAbove = 0;
cout << "Temperature of first day (-100 for end of input): ";
cin >> temp;
30
// loop
while (temp > -100)
{
if (temp > 20) // temperature above 20 degrees?
cin >> temp;
nrAbove++;
} // end of while loop
// results
cout << "Number of days with temperature above 20 degrees C is ";
cout << nrAbove << endl;
return 0;
}

-remove the ' 30 ' in your code.
-swap lines:cin >> temp; and nrAbove++;

Thanks people.

Narue
niek_e
Black Magic

It’s working well!

Complete code appears below.

// How many days with temperature above 20 degrees C?
#include <iostream>
using namespace std;
int main( )
{
float temp;
int nrAbove;
// initialize
nrAbove = 0;
cout << "Temperature of first day (-100 for end of input): ";
cin >> temp;
30;
// loop
while (temp > -100)
{
if (temp > 20) // temperature above 20 degrees?
cin >> temp;
nrAbove++;
} // end of while loop
// results
cout << "Number of days with temperature above 20 degrees C is ";
cout << nrAbove << endl;
return 0;
}

Nice to see it working.
So jw, but was it homework or just boredom :)?

remove the ' 30 ' in your code.
swap lines:cin >> temp; and nrAbove++;

Thanks I was about to post the following.

"Sorry guys I when I run the program it work's, when I try again it doesn't, it sorts of freeze's."

Now it works better.

Complete code appears below.

// How many days with temperature above 20 degrees C?
#include <iostream>
using namespace std;
int main( )
{
float temp;
int nrAbove;
// initialize
nrAbove = 0;
cout << "Temperature of first day (-100 for end of input): ";
cin >> temp;
// loop
while (temp > -100)
{
if (temp > 20) // temperature above 20 degrees?
nrAbove++;
cin >> temp;
} // end of while loop
// results
cout << "Number of days with temperature above 20 degrees C is ";
cout << nrAbove << endl;
return 0;
}
commented: How many times are you going to ignore "code tags" links? -3

Even though its solved here's a solution:

#include <iostream>
using namespace std;

int main()
{
    float temp = 0;
    int countDays = 0;

    while(temp > -100)
    {
               cout << "Temperature of first day (-100 for end of input): ";
               cin >> temp;
               if(temp > 20)
               {
                       countDays++;
               }
               else
               {
               }
    }

    cout << "Number of days with temperature above 20 degrees C is " << countDays << endl;
    system("PAUSE");
    return 0;
}
commented: A code tag demerit for you as well. -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.