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"

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

Edited 3 Years Ago by Reverend Jim: Fixed formatting

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

Edited 3 Years Ago by Reverend Jim: Fixed formatting

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

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

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Comments
How many times are you going to ignore "code tags" links?

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

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Comments
A code tag demerit for you as well.
This question has already been answered. Start a new discussion instead.