c++ arrays help

Jul 3, 2012 at 12:52pm
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;
}
}
Last edited on Jul 3, 2012 at 12:53pm
Jul 3, 2012 at 1:16pm
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:

1
2
3
cout << "Rainfall for month ";
printMonth(i);
cout << ": " << avgmonth[i];




Jul 3, 2012 at 1:27pm
your code was awful.i created a little.

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <string>
#include <cmath>
using namespace 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;
}

}
Jul 3, 2012 at 1:27pm
think that i helped.
Jul 3, 2012 at 2:12pm
thanks guys appreciate it
Topic archived. No new replies allowed.