what is the problem with this program

I don't know why this program doesn't work
my c++ shows me that there is one error.

the question is

(Write a program that reads in the average monthly rainfall for a city for each month of the year and then reads in the actual monthly rainfall for each of the previous 12 months. The program then prints out a nicely formatted table showing the rainfall for each of the previous 12 months as well as how much above or below average the rainfall was for each month. The average monthly rainfall is given for the months January, February, and so forth, in order. To obtain the actual rainfall for the previous 12 months, the program first asks what the current month is and then asks for the rainfall figures for the previous 12 months. The output should correctly label the months.)

and here are the codes

#include <iostream.h>
#include <iomanip.h>
void printMonth(int month);
// PRECONDITION: month holds an integer 1-12
// POSTCONDITION: the corresponding month (Jan, Feb, ..., Dec) has been printed to the standard output.
int main()
{
double rainfall[12]; //this year's rainfall for each month
double averages[12]; //average rainfalls for each month
int currentMonth; //what month is it? 1-based
// Get the average rainfall for each month, Jan-Dec
cout << "Please enter average rainfall for each month" << endl;
for (int i=0; i<12; i++)
{
printMonth(i);
cout << ": ";
cin >> averages[i];
}
// Get the actual rainfall for the previous year; first have to ask what month it is to know what month to start with.
cout << "What is the number of the current month? Jan=1, Feb=2, etc." << endl;
cin >> currentMonth;
cout << "Please enter the rainfall for each month in the previous year" << endl;
int count = 0;
for (int month=currentMonth-1; count < 12; month=(month+1)%12, count++)
{ printMonth(month);
cout << ": ";
cin >> rainfall[month];
}
// Print table showing avgs, actual rainfalls, and deviations from avg for previous 12 months.

void print_month(std::string name, double rainfall,double averages,int currentMonth) {
std::cout.setf(std::ios_base::left);
std::cout << setw(20) << name << setw(20)<< rainfall << setw(20) << averages << setw(20)<< currentMonth
std::endl;
}

} void printMonth(int month)
{
cout.width(8);
switch(month)
{
case 0:
cout << "Jan";
break;
case 1:
cout << "Feb";
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 << "Aug";
break;
case 8:
cout << "Sept";
break;
case 9:
cout << "Oct";
break;
case 10:
cout << "Nov";
break;
case 11:
cout << "Dec";
break;
}
} // End of Question #1
Code tags, please. Also, if you could, post what it's doing that's wrong.
The program shows in the output window



1-In function 'int main()':
2-Line 31: error: a function-definition is not allowed here before '{' token
3-compilation terminated due to -Wfatal-errors.

You're missing a closing bracket for int main. The last closing bracket you have only closes the last for loop.
curious why do you have your functions in your main they need to be out side your main() either beginning or end. Should indent to to make your code more legible. In example
1
2
3
4
5
6
main()
{
        cout << "Hello world";
        system("pause");
        return EXIT_SUCCESS;
}
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
#include <iostream.h>
#include <iomanip.h>
void printMonth(int month);
// PRECONDITION: month holds an integer 1-12
// POSTCONDITION: the corresponding month (Jan, Feb, ..., Dec) has been printed to the standard output.
int main()
{
double rainfall[12]; //this year's rainfall for each month
double averages[12]; //average rainfalls for each month
int currentMonth; //what month is it? 1-based
// Get the average rainfall for each month, Jan-Dec
cout << "Please enter average rainfall for each month" << endl;
for (int i=0; i<12; i++)
{
printMonth(i);
cout << ": ";
cin >> averages[i];
}
// Get the actual rainfall for the previous year; first have to ask what month it is to know what month to start with.
cout << "What is the number of the current month? Jan=1, Feb=2, etc." << endl;
cin >> currentMonth;
cout << "Please enter the rainfall for each month in the previous year" << endl;
int count = 0;
for (int month=currentMonth-1; count < 12; month=(month+1)%12, count++)
{ printMonth(month);
cout << ": ";
cin >> rainfall[month];
}
// Print table showing avgs, actual rainfalls, and deviations from avg for previous 12 months.

void print_month(std::string name, double rainfall,double averages,int currentMonth) {
std::cout.setf(std::ios_base::left);
std::cout << setw(20) << name << setw(20)<< rainfall << setw(20) << averages << setw(20)<< currentMonth
std::endl;
}

} void printMonth(int month)
{
cout.width(8);
switch(month)
{
case 0:
cout << "Jan";
break;
case 1:
cout << "Feb";
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 << "Aug";
break;
case 8:
cout << "Sept";
break;
case 9:
cout << "Oct";
break;
case 10:
cout << "Nov";
break;
case 11:
cout << "Dec";
break;
}
} // End of Question #1  


Here is the coded version, I know it sucks to read.
you forgot using namespace std; after your include files
I took the trouble to indent his code...

http://pastebin.com/n7eFrMhB

Line 38 looks fishy...
Topic archived. No new replies allowed.