How to wrap integer if greater than a certain number?

Program asks for current month, the rainfall and the average rainfall for each of the 12 previous months & puts them into a chart, then you can switch between chart and graph view. Everything works except in the graph, the rainfalls and average rainfalls always start with january's numbers. It's supposed to start with the current month's numbers then go in order. How do I get the numbers to start at the current month and wrap around? Like if the month is august it might go like 8 9 10 11 12 1 2..

(cut beginning part because code is too long)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209

int main()
{
	char input = 'Y';
	do
	{
		int currentMonth;
		double rainfall[11];
		double avg[11];
		int count2 = 0;
		int month;
		int countfunction = 0;
		input = 't';
		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 << "\nPlease 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 << "\nPlease 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];
		}
		cout << " " << endl;
		system("pause");
		system("cls");


		tableChart(avg, rainfall, month, countfunction, monthsOfYear);
		cout << "\nEnter t to see table view or g to see graph view or enter y to restart: " << endl;
		cin >> input;
		cout << "\n";
		system("cls");

		while (input == 't' || input == 'g')
		{

			{

				if (countfunction == 12)
					countfunction = 0;
				while (input == 'g')
				{
					graph(avg, rainfall, month, countfunction);
					cout << "\nEnter t to see table view or g to see graph view or y to restart:  \n";
					cin >> input;
					system("cls");
					if (countfunction == 12)
						countfunction = 0;
				}
				while (input == 't')
				{
					tableChart(avg, rainfall, month, countfunction, monthsOfYear);
					cout << "\nEnter t to see table view or g to see graph view or y to restart: \n";
					cin >> input;
					cout << "\n" << endl;
					system("cls");
					if (countfunction == 12)
						countfunction = 0;
				}
			}

		} while (input == 't' || input == 'g');
	} while (input == 'y');
}

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.fill('_');
	cout.width(8);
	switch (month)
	{
	case 0:
		cout << left << "Jan";
		break;

	case 1:
		cout << left << "Feb";
		break;

	case 2:
		cout << left << "March";
		break;

	case 3:
		cout << left << "April";
		break;

	case 4:
		cout << left << "May";
		break;

	case 5:
		cout << left << "June";
		break;

	case 6:
		cout << left << "July";
		break;

	case 7:
		cout << left << "Aug";
		break;

	case 8:
		cout << left << "Sept";
		break;

	case 9:
		cout << left << "Oct";
		break;

	case 10:
		cout << left << "Nov";
		break;

	case 11:
		cout << left << "Dec";
		break;
	}
}
void swapValues(string& v1, string& v2)
{
	string switcharoo;
	switcharoo = v1;
	v1 = v2;
	v2 = switcharoo;

}
//Chart starts here. the values that are wrong are rainfall[1], rainfall[2], rainfall[3].. and avg[1], avg[2], avg[3]...
void tableChart(double avg[], double rainfall[], int& month, int& 
countfunction, string monthsOfYear[])

{
	int i = month;
	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[i] << 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[i] << 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[i] << 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[i] << 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[i] << 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[i + 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[i + 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[i + 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[i + 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[i + 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[i + 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[i + 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;)
	{
		outputMonth(month);
		cout << " " << endl;
		cout << "Rainfall: ";
		printAsterisks(rainfall[i]);
		cout << "Average: ";
		printAsterisks(avg[i]);
		i = (i + 1) % 12;
		counter2++;
		cout << " " << endl;
		month = (month + 1) % 12;
	}
}

void printAsterisks(double n)
{
	for (int count = 0; count < n; count++)
	{
		cout << "*";
	}
	cout << endl;
}
closed account (48T7M4Gy)
Use modular arithmetic. eg If starting month is 8 August and you go 20 months then the 28th month is 28 % 12 which is 4 i.e. April
Last edited on
1
2
3
4
5
int currentMonth = 7;
for (int i = 0; i<12; ++i) {
    int month = (currentMonth+i) % 12;
    cout << month << '\n';
}

kemort I can't get it to work without the last month values come out as hexidecimal.
dhayden how do I make it so it takes the currentMonth that the user inputs? like not just 7 but whatever month number they entered.

never mind. I spent hours trying to understand this and I have a headache so I give up. -_-
Last edited on
I was just setting currentMonth=7 as an example. Simply delete that line and use whatever value of currentMonth the user input.
closed account (48T7M4Gy)
Keep in mind first month is monthsOfYear[0]

Note: hexadecimal isn't relevant here

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

int main()
{
  std::string monthsOfYear[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  
  int month = 0;
  
  for (int i = 0; i < 24; i++)
  {
     std::cout << i << '\t' << monthsOfYear[i % 12] << std::endl;
  }
  
  std::cout << "Enter a month number: ";
  std::cin >> month;
  std::cout << " = " << monthsOfYear[ month % 12 ] << std::endl;
}

Last edited on
I was setting currentMonth=7 as an example. Just delete that line and use whatever value of currentMonth the user input.
Topic archived. No new replies allowed.