Rainfall Statistics

Our assignment is to create a program that a) allows a user to enter a positive number (type double) into an array, one for each month b) calculates total, lowest value, highest value and average. I've seen this assignment mentioned several times on this forum. Actually, I could probably locate the full source for it - but then what would I learn?

I can get this program to work perfectly. I have that moment of satisfaction, and then I run it again because I want to see test new values. I always start with January @ 1 for consistency. When I rerun the program, it breaks. This makes me think it is a memory issue, and that makes me think I am doing the string array wrong (we haven't really learned much about string arrays yet, mostly numeric or char).

I really want to learn to program. I am not going to let this get discouraged. if anything, this experience has led me to join this forum. I hope to be here for a while. With no further adieu, here is my code:
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
# include <iostream>
# include <cstring>
using namespace std;

int main () 
	
{	double avgRain = 0;
	double rainSum = 0;
	int count = 0;
	double monthlyTotals[11];
	string monthNames[] = {"January","Febuary","March","April","May","June","July","August","September","October","November","December"};
	cout << "Please enter the amount of rainfall for each month, and press enter after each: " << endl;
	for (count = 0; count <= 11; count++)
	{
		cout << monthNames[count] << " : ";
		cin >> monthlyTotals[count];
		while (monthlyTotals[count] < 0)
		{
			cout << "Please reenter a positive number for the month of " << monthNames[count] << endl;
			cin >> monthlyTotals[count];
		}
	}
	for (count =0; count <=11; count++)
		rainSum = rainSum + monthlyTotals[count];
	avgRain = rainSum / 12;
	for (count = 0; count <=11; count++)
	{	cout << "Output : " << endl;
		cout << monthNames[count] << "\t" << monthlyTotals[count] << endl;
	}
	cout << rainSum << "is the total rainfall for the year\n" << avgRain << " is the average for the year.\n";
	return 0;
}


I would like to know, obviously, what I am doing wrong. I know the code gets real ugly at the bottom. I am not really worried about the calculations yet, and through those in for testing. Right now I am concerned with entering and retrieving the values. I am using the same subscript for both arrays in order to keep the name of the month tied to the numeric data.
double monthlyTotals[11];

The size needs to be 12, not 11.
I made that change, although my overall problem is still there.

Also, when creating the size of an array, it is the number of values? I had originally done 11 so that it could be 0-11, for a total of 12. But I see how it makes more sense to be 12 total values starting at 0, so at least I wont make that mistake any more.

Can anyone tell me what is wrong with the rest of my code?
I think I may have resolved it. I love xCode, but I hate macs. Pressing the "enter" key by the num pad would not really submit my value. Has to be the "return" key, instead.

Thank you for your help Archaic, I forgot to mention that in my last post.
With the exception of commenting, I have finally finished my program. This is for my final, which is on Monday - so while I love criticism, I will not make any further changes. I don't think solving the problem I had earlier is cheating, but I think any major improvements to what I have done would be.

The problem is this:
Write a program that lets the user enter the total rainfall for each of 12 months into an array of doubles. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months wit hthe highest and lowest amounts.
Input Validation: Do not accept negative numbers for monthly rainfall figures.


I just realized how I could have done the input validation better, ie requiring the number to be within a valid limit instead of excluding invalid numbers. However, I think I have met the requirements - and I promised myself no changes after this point anyway. Here is the code I came up with to solve this problem:
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
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;

void tableFormat(string,string,string,string);
void tableFormat(double,double,double,double);
int main () 
	
{	double avgRain = 0;
	double rainSum = 0;
	int count = 0;
	double monthlyTotals[12];
	string outRainSum;
	double lowpoint=100000;
	double highpoint=0;
	string lowMonth;
	string highMonth="January";
	string monthNames[] = {"January","Febuary","March","April","May","June","July","August","September","October","November","December"};
	cout << "Please enter the amount of rainfall for each month, and press enter after each: " << endl;
	for (count = 0; count <= 11; count++)
	{
		cout << monthNames[count] << " : ";
		cin >> monthlyTotals[count];
		while (monthlyTotals[count] < 0)
		{
			cout << "Please reenter a positive number for the month of " << monthNames[count] << endl;
			cin >> monthlyTotals[count];
		}
	}
		
	
	for (count =0; count <=11; count++)
		rainSum = rainSum + monthlyTotals[count];
	avgRain = rainSum / 12;
	for (count = 0; count <=11; count++)
	{	cout << endl;
		cout << monthNames[count] << "\t" << monthlyTotals[count] << endl;
	}
	highpoint = monthlyTotals[0];	
	for (count=0; count<=11; count++) 
	{
	if (monthlyTotals[count] >= highpoint)
	{
		highpoint = monthlyTotals[count];
		highMonth = monthNames[count];
	}
	}
	lowpoint = monthlyTotals[0];	
	for (count=0; count<=11; count++) 
	{
		if (monthlyTotals[count] <= lowpoint)
		{
		lowpoint = monthlyTotals[count];
		lowMonth = monthNames[count];
		}
	}
	tableFormat("Total","Average","Lowpoint","Highpoint");
	tableFormat(rainSum, avgRain, lowpoint,highpoint);
	cout << "The month(s) with the lowest rainfall is ";
	for (count=0; count <=11; count++)
	 
		if (monthlyTotals[count] == lowpoint)
			cout << monthNames[count] << ", ";
	
	cout << "with a rainfall of: "<< lowpoint << "." << endl;
	cout << "The month(s) with the highest rainfall is ";
	for (count=0; count <=11; count++)
		
		if (monthlyTotals[count] == highpoint)
			cout << monthNames[count] << ", ";
	
	cout << " with a rainfall of: "<< highpoint <<"." << endl;
	
	return 0;
}

void tableFormat(string colA,string colB,string colC,string colD)
{
	cout <<setw(15) << fixed <<  colA <<setw(15) << fixed <<  colB <<setw(15) << fixed <<  colC <<setw(15) << fixed <<  colD <<endl;
}
void tableFormat(double colA,double colB,double colC,double colD)
{
	cout << setprecision(3)<<setw(15) << fixed <<  colA <<setw(15) << fixed <<  colB <<setw(15) << fixed <<  colC <<setw(15) << fixed <<  colD << "stole this from C++.com" << endl;
}



PS If you are in my class, please do not copy my work. This was a fun and challenging project - and I think I learned a thing or two. For example, how to extract just the month names that were necessary for the high and low months. I would have liked to have formatted that better, however. Maybe a function overloaded 12 times.
Ok, new problem and I'm panicking just a little bit. I wrote this in xCode, but it will be checked on a PC running visual studio 2005. It compiles perfectly on my Mac, but not at all on my PC. I get the following REALLY long list of errors:
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
1
1>Compiling...
1>stdafx.cpp
1>Compiling...
1>Rainfall Statistics.cpp
1>c:\users\toshiba\documents\visual studio 2005\projects\rainfall statistics\rainfall statistics\rainfall statistics.cpp(27) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
1>        c:\program files\microsoft visual studio 8\vc\include\ostream(656): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files\microsoft visual studio 8\vc\include\ostream(703): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files\microsoft visual studio 8\vc\include\ostream(741): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files\microsoft visual studio 8\vc\include\ostream(788): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files\microsoft visual studio 8\vc\include\ostream(912): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const signed char *)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files\microsoft visual studio 8\vc\include\ostream(919): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files\microsoft visual studio 8\vc\include\ostream(926): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)'
--------------------------------------------------------------------------------Truncated------------------------------------------------------------------
1>            _Traits=std::char_traits<char>
1>        ]
1>        while trying to match the argument list '(std::basic_ostream<_Elem,_Traits>, std::string)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>Build log was saved at "file://c:\Users\Toshiba\Documents\Visual Studio 2005\Projects\Rainfall Statistics\Rainfall Statistics\Debug\BuildLog.htm"
1>Rainfall Statistics - 6 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Replace:

#include <cstring>

with:

#include <string>
I hope you guys aren't getting tired of me. Apparently, xCode requires <cstring> while VS requires <string>. I'm compiled and finally about to submit.
Archaic - How did I know you would be to my rescue?

I have to ask. I'm (obviously) just starting out. It took me since last night to notice that, but you posted the correct solution within minutes. How long does it take to get from here to there, and do you have any recommendations for how to do it?
After awhile you begin to recognize errors and what could possibly be causing them. It's really just a matter of exposure and experience. In this particular case your code worked in one compiler and not another, so I looked at the libraries you had included and cstring stood out. I was 99.99% sure that was the problem, but just to make sure I didn't give bad advice, I tossed the code into Visual Studio and checked to make sure I was right.

Topic archived. No new replies allowed.