hi guys i have written a code to compare the rainfall of last and current year and find the difference. can someone please tell me where is if any mistake.
//This program reads in the average monthly rainfall for each month of the year and then reads in the //actual monthly rainfall for each of the previous 12 months.
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
void printMonth(int month);
int main()
{
int month[12];
int avgmonth[12];
int actmonth[12];
float difference;
int i;
Cout<<"enter the average rainfall for each month"<<endl;
For (int i=o; i<12; i++)
{
Cout<<month[i]<<" : ";
Cin>>avgmonth[i];
}
Cout<<"enter the actual rainfall for each month"<<endl;
For (int i=0; i<12; i++)
Cout<<month[i]<," : ";
Cin>>actmonth[i];
}
For (int i=0; i<12; i++)
{
cout<<month[i]<<endl;
<<"difference : "<<(actMonth[i]-avgMonth[i])<<endl<<endl;
void printMonth(int month)
{
Switch (month )
{
Case 0:
Cout<<"January";
break;
case 1:
cout<<"February";
break;
case 2:
cout<<"march";
break;
case 3:
cout<<"april";
break;
case 4:
cout<<"may";
break;
case 5:
cout<<"june";
break;
case 6:
cout<<"july";
break;
case 7:
cout<<"august";
break;
case 8:
cout<<"September";
break;
case 9:
cout<<"october";
break;
case 10:
cout<<"November";
break;
case 11:
cout<<"December";
break;
}
}
Instead of the switch, make an array of strings (or an enum type) that holds each month. You can use the switch if you want though.
Now, month[i] is never assigned a value, making it useless. Based on your switch function, you're probably going to set month[i] = i somewhere, which is redundant.
So, either change it to this:
std::string month[12] = {"January", "February", ... };
Or get rid of month[] and do this:
#include <iostream>
#include <string>
#include <cmath>
usingnamespace std;
int printMonth(int month1);
int main()
{
int avgmonth;
int actmonth;
cout<<"enter the average rainfall for each month"<<endl;
cin>>avgmonth;
printMonth(avgmonth);
cout<<"\nenter the actual rainfall for each month"<<endl;
cin>>actmonth;
printMonth(actmonth);
int difference=actmonth-avgmonth;
cout<<"\ndifference : "<<difference<<endl<<endl;
printMonth(difference);
}
int printMonth(int month1)
{
switch(month1)
{
case 0:
cout<<"January";
break;
case 1:
cout<<"February";
break;
case 2:
cout<<"march";
break;
case 3:
cout<<"april";
break;
case 4:
cout<<"may";
break;
case 5:
cout<<"june";
break;
case 6:
cout<<"july";
break;
case 7:
cout<<"august";
break;
case 8:
cout<<"September";
break;
case 9:
cout<<"october";
break;
case 10:
cout<<"November";
break;
case 11:
cout<<"December";
break;
}
}