Trouble with code

I got Part 1 to work, but having trouble with Part 2 of the problem.


Question:
Part 1:
The program outputs a table of the last 12 months rainfall and average rainfall, and how much above or below average the rainfall was for each month. First it asks the current month and has the user input the actual rainfall for each month and the average rainfall for each month.

Part 2:
After you have completed the previous program, produce an enhanced version that also outputs a graph showing the average rainfall and the actual rainfall for each of the previous 12 months. There should be two bar graphs for each month and they should be labeled as the average rainfall and the rainfall for the most recent month. Your program should also ask whether the user wants to see the table or the bar graph, and then should display whichever format is requested. Include a loop that allows the user to see either format as often as the user wishes until the user requests that the program end.

(The graph should print asterisks to show the bars. For example if the rainfall for January was 5 and the average was 7 it would print:
"Jan:
Rainfall *****
Average: *******"

and I can't figure out how to switch between the tablechart and graph. The program goes wonky when inputting 't' for table and 'g' for graph.



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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

#include <iostream>
#include <string>
#include<iomanip>
using namespace std;

string printMonth(int& month, string date[], int& counter);
void swapValues(string& v1, string& v2);
void outputMonth(int month);
void tableChart(double avg[], double rainfall[], int& month, int& countfunction, string monthsOfYear[]);
void graph(double avg[], double rainfall[], int& month, int& countfunction);
void printAsterisks(double n);

int main()
{

	int currentMonth;
	double rainfall[11];
	double avg[11];
	int count2 = 0;
	int month;
	int countfunction = 0;
	string monthsOfYear[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };


	//Input current month
	cout << "What is the number of the current month? Jan = 1, Feb = 2, etc." << endl;
	cin >> currentMonth;
	month = currentMonth - 1;


	//Input average rainfall for each month
	cout << "Please enter average rainfall for each month" << endl;
	for (int i = currentMonth - 1; count2 < 12; i = (i + 1) % 12, count2++)
	{
		outputMonth(i);
		cout << ": ";
		cin >> avg[i];
	}


	//Input rainfall for each month
	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++)
	{
		outputMonth(month);
		cout << ": ";
		cin >> rainfall[month];
	}

	tableChart(avg, rainfall, month, countfunction, monthsOfYear);

	char input;
	do
	{
	
		{
			cout << "Enter t to see table view or g to see graph view.  ";
			cin >> input;
			if (input == 't')
			{
				graph(avg, rainfall, month, countfunction);
			}
			if (input == 'g')
			{
				tableChart(avg, rainfall, month, countfunction, monthsOfYear);
			}
		}

	} while (input == 's');
}

string printMonth(int& month, string date[], int& counter)
{
	for (int i = month; counter < 12;)
	{
		month = (month + 1) % 12;
		counter++;
		return date[i];
	}
}

void outputMonth(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;
	}
}
void swapValues(string& v1, string& v2)
{
	string switcharoo;
	switcharoo = v1;
	v1 = v2;
	v2 = switcharoo;

}

void tableChart(double avg[], double rainfall[], int& month, int& countfunction, string monthsOfYear[])

{
	cout << setfill('-') << setw(1) << "+" << setw(10) << "-" << setw(1) << "+" << setw(10) << "-" << setw(1) << "+" << setw(10) << "-" << setw(1) << "+" << setw(10) << "-" << setw(1) << "+" << endl;
	cout << setfill(' ') << setw(1) << "|" << setw(10) << left << "Month" << "|" << setw(10) << left << "Rainfall" << setw(1) << "|" << setw(10) << left << "Average" << setw(1) << "|" << setw(1) << left << "Difference" << setw(1) << "|" << endl;
	cout << setfill('-') << setw(1) << "+" << setw(10) << "-" << setw(1) << "+" << setw(10) << "-" << setw(1) << "+" << setw(10) << "-" << setw(1) << "+" << setw(10) << "-" << setw(1) << "+" << endl;
	cout << setfill(' ') << setw(1) << "|" << setw(10) << left << printMonth(month, monthsOfYear, countfunction) << "|" << setw(10) << left << rainfall[0] << setw(1) << "|" << setw(10) << left << avg[0] << setw(1) << "|" << setw(10) << left << rainfall[0] - avg[0] << setw(1) << "|" << endl;
	cout << setfill(' ') << setw(1) << "|" << setw(10) << left << printMonth(month, monthsOfYear, countfunction) << "|" << setw(10) << left << rainfall[1] << setw(1) << "|" << setw(10) << left << avg[1] << setw(1) << "|" << setw(10) << left << rainfall[1] - avg[1] << setw(1) << "|" << endl;
	cout << setfill(' ') << setw(1) << "|" << setw(10) << left << printMonth(month, monthsOfYear, countfunction) << "|" << setw(10) << left << rainfall[2] << setw(1) << "|" << setw(10) << left << avg[2] << setw(1) << "|" << setw(10) << left << rainfall[2] - avg[2] << setw(1) << "|" << endl;
	cout << setfill(' ') << setw(1) << "|" << setw(10) << left << printMonth(month, monthsOfYear, countfunction) << "|" << setw(10) << left << rainfall[3] << setw(1) << "|" << setw(10) << left << avg[3] << setw(1) << "|" << setw(10) << left << rainfall[3] - avg[3] << setw(1) << "|" << endl;
	cout << setfill(' ') << setw(1) << "|" << setw(10) << left << printMonth(month, monthsOfYear, countfunction) << "|" << setw(10) << left << rainfall[4] << setw(1) << "|" << setw(10) << left << avg[4] << setw(1) << "|" << setw(10) << left << rainfall[4] - avg[4] << setw(1) << "|" << endl;
	cout << setfill(' ') << setw(1) << "|" << setw(10) << left << printMonth(month, monthsOfYear, countfunction) << "|" << setw(10) << left << rainfall[5] << setw(1) << "|" << setw(10) << left << avg[5] << setw(1) << "|" << setw(10) << left << rainfall[5] - avg[5] << setw(1) << "|" << endl;
	cout << setfill(' ') << setw(1) << "|" << setw(10) << left << printMonth(month, monthsOfYear, countfunction) << "|" << setw(10) << left << rainfall[6] << setw(1) << "|" << setw(10) << left << avg[6] << setw(1) << "|" << setw(10) << left << rainfall[6] - avg[6] << setw(1) << "|" << endl;
	cout << setfill(' ') << setw(1) << "|" << setw(10) << left << printMonth(month, monthsOfYear, countfunction) << "|" << setw(10) << left << rainfall[7] << setw(1) << "|" << setw(10) << left << avg[7] << setw(1) << "|" << setw(10) << left << rainfall[7] - avg[7] << setw(1) << "|" << endl;
	cout << setfill(' ') << setw(1) << "|" << setw(10) << left << printMonth(month, monthsOfYear, countfunction) << "|" << setw(10) << left << rainfall[8] << setw(1) << "|" << setw(10) << left << avg[8] << setw(1) << "|" << setw(10) << left << rainfall[8] - avg[8] << setw(1) << "|" << endl;
	cout << setfill(' ') << setw(1) << "|" << setw(10) << left << printMonth(month, monthsOfYear, countfunction) << "|" << setw(10) << left << rainfall[9] << setw(1) << "|" << setw(10) << left << avg[9] << setw(1) << "|" << setw(10) << left << rainfall[9] - avg[9] << setw(1) << "|" << endl;
	cout << setfill(' ') << setw(1) << "|" << setw(10) << left << printMonth(month, monthsOfYear, countfunction) << "|" << setw(10) << left << rainfall[10] << setw(1) << "|" << setw(10) << left << avg[10] << setw(1) << "|" << setw(10) << left << rainfall[10] - avg[10] << setw(1) << "|" << endl;
	cout << setfill(' ') << setw(1) << "|" << setw(10) << left << printMonth(month, monthsOfYear, countfunction) << "|" << setw(10) << left << rainfall[11] << setw(1) << "|" << setw(10) << left << avg[11] << setw(1) << "|" << setw(10) << left << rainfall[11] - avg[11] << setw(1) << "|" << endl;
	cout << setfill('-') << setw(1) << "+" << setw(10) << "-" << setw(1) << "+" << setw(10) << "-" << setw(1) << "+" << setw(10) << "-" << setw(1) << "+" << setw(10) << "-" << setw(1) << "-" << endl;
	return;
}

void graph(double avg[], double rainfall[], int& month, int& countfunction)
{
	int counter2 = 0;
	for (int i = month; counter2 < 12; counter2++, i++)
	{
		outputMonth(month);
		cout << ":" << endl;

		cout << "Rainfall: ";
		printAsterisks(rainfall[i]);
		cout << "Average: ";
		printAsterisks(avg[i]);
		month = (month + 1) % 12;
	}
}
void printAsterisks(double n)
{
	for (int count = 1; count <= n; count++)
		cout << "*";
}



Topic archived. No new replies allowed.