Help with inititalizing a variable in a for loop
It says total rain is not initialized, but if I set it to 0 then it will be reset every time it loops around how can I fix this?
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
#include <iostream>
#include <string>
using namespace std;
struct months
{
float rain;
float high;
float low;
float average;
};
int main()
{
const int month= 12;
months total[month];
string monthName[month]={"January","February","March","April","May","June","July","August",
"September","October","November","December"};
for(int i=0; i<12; i++){;
cout << monthName[i] << endl;
cout << endl;
cout << "Enter total rain for this month: ";
cin >> total[i].rain;
cout << endl;
cout << "Enter the highest temperature for this month: ";
cin >> total[i].high;
while(total[i].high<-100||total[i].high>140)
{
cout << endl;
cout<<"Invalid entry-must be between -100 through 140\n";
cout<<"high temperature: ";
cin>>total[i].high;
cout << endl;
}
cout << endl;
cout << "Enter the lowest temperature for this month: ";
cin >> total[i].low;
while(total[i].low<-100||total[i].low>140)
{
cout << endl;
cout<<"Invalid entry-must be between -100 through 140\n";
cout<<"high temperature: ";
cin>>total[i].low;
cout << endl;
}
cout << endl;
cout << endl;
total[i].average = (total[i].high+total[i].low)/2;
cout <<"The avearge temperature of this month is: " << total[i].average;
cout << endl;
cout << endl;
int totalRain;
totalRain += total[i].rain;
if(i==11){
cout << totalRain;
}
}
system("PAUSE");
return 0;
}
|
Last edited on
Does it give error at line 56? Line 55 should be int totalRain=0;
(need to initialize it), and you probably want it outside the for loop
Declare and initialize it outside your for loop?
This could be moved outside the loop too:
1 2 3
|
if(i==11){
cout << totalRain;
}
|
Move it after the end of the for loop, and you can just put:
cout << totalRain;
(though to be fair, some text to label the value would be useful too).
Topic archived. No new replies allowed.