while loop wont exit

IM using a while loop to solve a number pyramid.

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
double zerocount = 0, count = 0;
	system("pause");
	cout << "Initial display of the pyramid:" << endl << endl;

	//Displaying the pyramid using the function "display"
	display(np);

	//counting Zeros before solving
    zerocounter(np);

	cout << endl << endl << endl; //putting space after the pyramid
	cout << "The number of zeros starting the pyramid is: " << zerocount << endl;

	system("pause");

	while (zerocount != 0) //Begin giant while loop
	{
    //solver in the while loop using the "solver" function
	solver(np);

    //Displaying the pyramid using the function "display" in the while loop
	display(np);

	cout << endl << endl << endl; //putting space after the pyramid

	//Zero counter in the while loop using the function "zerocounter"
	zerocounter(np);

	count++;

	cout << "The number of passes through the pyramid so far is: " << count << endl << endl;

	system("pause");

	};

	cout << "Thanks for playing the pyramid game!" << endl;

	system("pause");

	return 0;
}

double zerocounter(int pzc[7][7])
{
	int i, j;
	double zerocount = 0;
	for (i = 0; i < maxr; i++)
	  { 
		for (j = 0; j < maxc; j++)
		{
			if(pzc[i][j] == 0)
				zerocount++;
		}
	  }
	cout << "The number of zeros remaining is: " << zerocount << endl;
	return zerocount;
}

void display(int pd[7][7])
{
	int i, j;
	for (i = 6; i >= 0; i--)
	{ 
		cout << endl;
		for (j = 0; j < maxc; j++)
		{
			if (pd[i][j] == -1)
				cout << "   ";
			else
				cout << setw(6) << pd[i][j];
		}
	}
	return;
}

int solver(int ps[7][7])
{
	int i, j;
	for (i = 6; i >= 0; i--)
		{ 
		for (j = 0; j < 6; j++)
		{
			 if (ps[i][j] != -1 && ps[i + 1][j + 1] == 0 && ps[i][j + 1] != 0 && ps[i][j] != 0)
			 ps[i + 1][j + 1] = ps[i][j + 1] + ps[i][j];

			 if (ps[i][j] != -1 && ps[i + 1][j + 1] != 0 && ps[i][j + 1] == 0 && ps[i][j] != 0)
			 ps[i][j + 1] = ps[i + 1][j + 1] - ps[i][j];			 

			 if (ps[i][j] != -1 && ps[i + 1][j + 1] != 0 && ps[i][j + 1] != 0 && ps[i][j] == 0)
			 ps[i][j] = ps[i + 1][j + 1] - ps[i][j + 1]; 
		}
	   }
	return ps[7][7];
}

For some reason after the pyramid is solved, it will display as having no zeros left but it will continue to run through the while loop.
You never seem to modify the value of "zerocount", so i'm not sure why you think this line:
while (zerocount != 0)
would ever test a different condition than the first test that gets you inside the loop.

edit:

perhaps line 27 needs to be:
 
zerocount = zerocounter(np);

?
Last edited on
Yes thats what it was, and I actually figured it out right before i checked this, but thanks!
no problem mate.
one other thing: why have you declared zerocount as a double inside that function? Seeing as you only increment it, i'd turn it into an integer, and also make the function return an integer.
Topic archived. No new replies allowed.