Help with initialising??

I do not know how I would go about initialising nrAbove in order to display a correct output?? Which would be displaying all the temperatures above 20 degrees c? Pls help



#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)
{
cout << "Enter the next temperature: ";
cin >> temp;
}

if (temp > 20)
{
nrAbove++;
}


// Results
cout << "Number of days with temperature above 20 degrees c is " << nrAbove << endl;

return 0;
}
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;

int main( )
{
    float temp = 0.0;
    int nrAbove = 0;

    cout << "Temperature of first day (-100 for end of input): ";
    cin >> temp;

    // Loop
    while (temp > -100)
   {
        cout << "Enter the next temperature: ";
        cin >> temp; 
        
        //check for days above 20 has been moved inside the while loop
        // to check each time a new value is added.
        if (temp > 20)
       {
            nrAbove++;
       } 
   }

   // Results
   cout << "Number of days with temperature above 20 degrees c is " << nrAbove << endl;

    return 0;
}
Last edited on
Also, I might note that initialization is different from assignment:
1
2
3
int nrAbove; //nrAbove is not initialized
nrAbove = 0; //assign the value 0 to nrAbove
nrAbove = 1; //assign the value 1 to nrAbove 


1
2
int nrAbove = 0; //initialize nrAbove to 0
nrAbove = 1; //assign the value 1 to nrAbove 


Those are probably horrible examples, but they illustrate the concept, right? ^_^

rpgfan
well initialization can be done at the beginning....in the declaration part as well....n for the incorrect output....i can see that the if-condition should be placed inside the while loop!!!!
Topic archived. No new replies allowed.