Average Rainfall

Pages: 12
So at the bottom where I need to calculate the total inches or rainfall, does that need to be totalInches = totalInches or just inches?
I tried both. I'm still getting build errors.
you don't need to do anything because you already have it (totalInches) ... Just output totalInches as the total inches of rainfall (:
string outRainSum;
string monthNames[];

you dont need string outRainSum;

and change string to char *, or insert #include <string> after line #include <iostream>
Like 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
#include <iostream>
using namespace std;

int main()
{
	int years;

	// Get the amount of years.
	cout << "Enter how many years of rainfall: ";
	cin >> years;

	double avgRain = 0;
	double totalInches = 0;
	int count = 0;
	char monthNames[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
	cout << "Please enter the amount of rainfall for each month: " << endl;
	for (int i=0; i<years; i++)
	{
		for (int j=0; j<12; j++)
		{
			cout << "Amount of rainfall for " << monthNames[j] << ": ";

			cin >> inches;

			totalInches += inches;
		}
	}

	// Calculate the total number of months.
	totalMonths = years * 12;

	// Display the total number of months.
	cout << "Total number of months: " << totalMonths << endl;

	// Calculate the total inches of rainfall.
	totalInches = inches;

	// Display the total inches of rainfall.
	cout << "Total inches of rainfall: " << totalInches << endl;

	// Calculate the average rainfall per month.
	average = totalInches / totalMonths;

	// Display the average rainfall per month.
	cout << "Average rainfall per month: " << average << endl;

	return 0;
}
You cannot just insert names into your code and expect the compiler to define variables for you.

cin >> inches => where is inches defined?
totalMonths = years*12 => where is totalMonths defined?
totalInches = ?? => where is totalInches defined?
average = totalInces / totalMonths => where is average defined?

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
#include <iostream>
#include <string>

int main()
{
    const unsigned nMonths = 12 ;
    const std::string months[nMonths] =
    {
        "Jan", "Feb", "Mar", "Apr", "May", "Jun",
        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    };

    std::cout << "Enter the number of years you'd like to enter amounts for.\n" ;

    int years ;
    std::cin >> years ;

    int total = 0 ;
    for ( unsigned i=0; i < years ; ++i )
    {
        std::cout << "Year " << i+1 << '\n' ;

        for ( unsigned month=0; month < nMonths; ++month )
        {
            std::cout << '\t' << months[month] << ": " ;

            int amount ;
            std::cin >> amount ;

            total += amount ;
        }
    }

    std::cout << "Time period: " << years * nMonths << " months.\n" 
        << "Total rainfall: " << total << " inches\n"
        << "Average: " << static_cast<float>(total) / (years*nMonths)
        << " inches/month\n" ;
}


Note that if you have something like:
1
2
3
for (int j=0; j<12; j++)
    cout << "Amount of rainfall for " << monthNames[j] << ": ";
cin >> inches;


Only the first statement after the for statement is in the body of the for loop. Line 2 will execute 12 times, line 3 will execute once. Correct is (after defining inches, of course):

1
2
3
4
5
for (int j=0; j<12; j++)
{
    cout << "Amount of rainfall for " << monthNames[j] << ": ";
    cin >> inches;
}


So only leave out the braces if you want one statement in the body of the for loop. (Indentation is for readability - it means nothing to the compiler.)
H3avenlySoul's line 36: totalInches = inches;
No you don't need this line

and cire already posted the full anser...
Last edited on
:(
Ok I started over. I'm not worried about it saying the specific month because we haven't got to that yet and my teacher just said he would give extra points for that. I'm not worried about the extra points anymore. I just want to be able to enter a number 12 times for every year. I started by doing just a basic one that asked for a number 12 times and added them together and it worked. So I went back in to add the outer loop for the years and now it's saying build error. This is the most frustrating thing ever but this is what I have after I added the outer loop for the years:

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
#include <iostream>
using namespace std;

int main()
{
	int years;

	// Get the total years.
	cout << "How many years? ";
	cin >> years;

	// Variables
	int years, // To hold each year
		total = 0; // Accumulator, intialized with 0

	// Constant for the number of years
	const int MAX_YEARS = years;

	// Get the years and accumulate them.
	for (int counter = 0; counter < years; counter++)
	{
		// Variables
		int number, // To hold each number
			total = 0; // Accumulator, initialized with 0
		
		// Constant for the number of numbers
		const int MAX_NUMS = 12;
		
		// Get the numbers and accumulate them.
		for (int counter = 0; counter < MAX_NUMS; counter++)
		{
			cout << "Rainfall for the month: ";
			cin >> number;
			total += number;
		}
		// Display the total.
		cout << "The total rainfall is: " << total << endl;
	}
	return 0;
}
I'm aware that I don't have the calculations in the program yet. I'm just trying to take this step by step and get the loop part right now.
I can see a few reasons it won't compile

in line 13:

int years, // To hold each year
you've already declared years in line 6, and if you hadn't you should still end the statement with a semicolon ";"

line 17:

const int MAX_YEARS = years;
don't think you can assign a variable int to a constant.
in fact I think that's the defining characteristic of constants, you have to assign a constant value to them, compile time. It can't be reassigned in runtime.

in lines 23 and 27:

1
2
3
4
5
6
7
8
for (int counter = 0; counter < years; counter++)
	{
		// Variables
		int number, // To hold each number
			total = 0; // Accumulator, initialized with 0
		
		// Constant for the number of numbers
		const int MAX_NUMS = 12;

you're declaring variables within a for loop.. you can see why this won't work.

I think what you want is an array like
float monthly_rainfall[12];

I'm assuming you know how to access elements of an array, so after declaring the array we can start a for loop and use the "counter" as the [] argument:

1
2
3
4
5
6
7
8
for (int counter = 0; counter < 12; counter++)
{
    cout << "Rainfall for the month: ";
    cin >> number; // this also must be declared at top of main()
    monthly_rainfall[counter] = number;
    total += number;
... 
}

then you can do stuff like get the average after they've all been entered.

edit: also should some of this stuff be in float instead of int?
or are all rainfall amounts going to be integers? that wouldn't make much sense.
Last edited on
cPlusN00b wrote:
don't think you can assign a variable int to a constant.
in fact I think that's the defining characteristic of constants, you have to assign a constant value to them, compile time. It can't be reassigned in runtime.


A constant can't be reassigned, no. But there's no reason the value of the constant needs to be known at compile time unless you're using it for something which requires a compile-time constant, so you absolutely can initialize a constant to a value which is not known at run-time, otherwise we would never be able to pass const arguments to functions.

Topic archived. No new replies allowed.
Pages: 12