Stuck in loop? Program stopped.

So I modified my code according to helios corrections and then made some further modifications. Now I can't seem to see why but the program just freezes or maybe enters an infinite loop? This occurs right after line 31's output. I suspect it has something to do with the if statement but running through it in my head it seems correct.

So it seems to depend on the inputs it will either pause or go though. For example, hitting 1 and enter over and over will complete the program but entering 1 then 2 then 3 etc. until 15. Will pause the program.

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
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	double snowfallReport[7][2], snowAvg = 0.0;
	int x = 0;
	string month;

	cout << "This program will generate a snow report.\n";
	cout << "Enter the month: ";
	cin >> month;

	for (int rows = 0; rows < 7; rows++)
	{
		for (int columns = 0; columns < 2;)
		{
			cout << "\nEnter a day: ";
			cin >> snowfallReport[rows][columns];
			columns++;
			cout << endl << "Enter the corresponding snow fall for that day: ";
			cin >> snowfallReport[rows][columns];
			columns++;
		}
	}

	cout << "\n\n\nSnow report " << month << " " << snowfallReport[0][0] << " - " << snowfallReport[6][0];
	cout << "\nDate\tSnow Fall\n";

	for (int rows = 0; rows < 7; rows++)
		cout << snowfallReport[rows][0] << "\t" << snowfallReport[rows][1] << endl;

	for (int rows = 0; rows < 7; rows++)
		snowAvg = snowAvg + snowfallReport[rows][1];

	for (int rows = 0; rows < 7; rows++)
	{
		if (snowfallReport[rows][1] >= snowfallReport[x][1])
			rows = x;
	}
	cout << showpoint << fixed << setprecision(2) << "\n\nHeight snow fall is " << snowfallReport[x][0] << " on "
		 << snowfallReport[x][1] << "and the average snowfall is " << snowAvg / 7;
	return 0;
}
Last edited on
The error has nothing to do with the size of the variables. The compiler is telling you that you're accessing out of bounds of the array.

This line has undefined behavior:
 
cout << snowfallReport[rows][columns] << "\t" << columns++ << snowfallReport[rows][columns] << columns++ << endl;
The order of evaluation is undefined. It won't necessarily happen in
snowfallReport[rows][columns]
columns++
snowfallReport[rows][columns]
columns++
as you expect. The operations could be in any conceivable order. Also, note you were printing the value of columns, which I assume is not what you intended.
Replace the entire inner loop with simply
 
cout << snowfallReport[rows][0] << "\t" << snowfallReport[rows][1] << endl;

And on this line:
 
cout << "\n\nHeight snow fall is " << snowfallReport[x][1] << " on " << snowfallReport[x][2] << "and the average snowfall is " << snowAvg;
you wrote the column indices wrong. You're accessing indices 1 and 2, rather than 0 and 1.
That's embarrassing how easy it sounds once you explain it the correct way... I made a stupid mistake on line 45 I see that now. But wow, been trying to rack my brain around that whole inner loop. I appreciate the help.
I'm a big dumb dumb. I just swapped rows = x to be x = rows on line 39 and it works.
Don't put yourself down. We've all been there.
Topic archived. No new replies allowed.