loop

I need help with an infinite loop for some reason my stopper will not let it exit.

#include<iostream>

using namespace std;



int main()
{
int island[13][13]={{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }};;

int randomNum;

int count = 0;
int stopper = 0;
int x = 6;
int y = 0;

for (count = 0; count < 5000;)
{

int stopper = 0;
int x = 6;
int y = 0;


do
{

randomNum = rand() % 100 + 1;

if (randomNum <= 44)
{
island[x][y + 1];

if (island[x][y] == 2 || island[x][y] == 1)
{
island[x][y] -= 1;

}
else if (island[x][y] == -1)
{
stopper = -1;

count++;
}
else if (island[x][y] == 3)
{
stopper = 3;
count++;

}
}
else if (randomNum > 44 && randomNum <= 65)
{
island[x + 1][y];

if (island[x][y] == 2 || island[x][y] == 1)
{
island[x][y] -= 1;
}
else if (island[x][y] == -1)
{
stopper = -1;

count++;
}
else if (island[x][y] == 3)
{
stopper = 3;
count++;
}
}
else if (randomNum > 65 && randomNum <= 90)
{
island[x - 1][y];

if (island[x][y] == 2 || island[x][y] == 1)
{
island[x][y] -= 1;
}
else if (island[x][y] == -1)
{
stopper = -1;
count++;
}
else if (island[x][y] == 3)
{
stopper = 3;
count++;
}
}
else if (randomNum > 90 && randomNum <= 100)
{
island[x][y - 1];

if (island[x][y] == 2 || island[x][y] == 1)
{
island[x][y] -= 1;
}
else if (island[x][y] == -1)
{
stopper = -1;
count++;
}
else if (island[x][y] == 3)
{
stopper = 3;
count++;
}
}
else
{
cout << "error" << endl;
}



} while (stopper != 3 || stopper != -1);
cout << stopper << endl;




return 0;





}
It helps if you use code tags: http://www.cplusplus.com/articles/jEywvCM9/

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
#include <iostream>
#include <ctime> // you need this to use time(NULL)

using namespace std;

int main ( )
{
  srand ( time ( NULL ) ); // this initializes the random number generator

  int island[13][13]={{ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 },
            { -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,-1 },
            { -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,-1 },
            { -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,-1 },
            { -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,-1 },
            { -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,-1 },
            { 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 },
            { -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,-1 },
            { -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,-1 },
            { -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,-1 },
            { -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,-1 },
            { -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,-1 },
            { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 }};

  // you had declared these twice, so I removed the second declaration
  int randomNum, stopper = 0, x = 6, y = 0;


  for ( int count = 0; count < 5000; )
  {
    do
    {
      randomNum = rand ( ) % 100 + 1;

      if ( randomNum <= 44 )
      {
        island[x][y+1]; // this does nothing

        if ( island[x][y] == 2 || island[x][y] == 1 )
        {
          island[x][y] -= 1;
        }
      
        else if ( island[x][y] == -1 )
        {
          stopper = -1;

          ++count;
        }

        else if ( island[x][y] == 3 )
        {
          stopper = 3;

          ++count;
        }
      }

      else if ( randomNum > 44 && randomNum <= 65 )
      {
        island[x+1][y]; // this does nothing

        if ( island[x][y] == 2 || island[x][y] == 1 )
        {
          island[x][y] -= 1;
        }

        else if ( island[x][y] == -1 )
        {
          stopper = -1;

          ++count;
        }

        else if ( island[x][y] == 3 )
        {
          stopper = 3;

          ++count;
        }
      }

      else if ( randomNum > 65 && randomNum <= 90 )
      {
        island[x-1][y]; // this does nothing

        if ( island[x][y] == 2 || island[x][y] == 1 )
        {
          island[x][y] -= 1;
        }

        else if ( island[x][y] == -1 )
        {
          stopper = -1;

          ++count;
        }

        else if ( island[x][y] == 3 )
        {
          stopper = 3;

          ++count;
        }
      }

      else if ( randomNum > 90 && randomNum <= 100 )
      {
        island[x][y-1]; // this does nothing

        if ( island[x][y] == 2 || island[x][y] == 1 )
        {
          island[x][y] -= 1;
        }

        else if ( island[x][y] == -1 )
        {
          stopper = -1;

          ++count;
        }

        else if ( island[x][y] == 3 )
        {
          stopper = 3;

          ++count;
        }
      }

      else
      {
        cout << "error\n";
      }
    } while ( stopper != 3 || stopper != -1 ); // this should always be true, as stopper cannot equal both 3 and -1

    cout << stopper << '\n';
  }
}
while (stopper != 3 || stopper != -1);
This is always true. If stopper is 3, then stopper != -1 is true. If stopper is -1 then stopper != 3 is true. I suspect you want while (stopper != 3 && stopper != -1);
Well it's supposed to be a simulation of walking across the island where the 3 is the bridge and -1 is the water so I need it to stop when it gets to either of those not both. 2s are flowers that if stepped on I get charged $5 for stepping on 1 but if you step in the same block twice both flowers are dead so you don't get charged again. I need to keep track of how it ended. I've included more code and more comments, but it will not run now.

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>

using namespace std;




int main()
{
	
	//island to walk across -1 is water, 3 is a bridge, 0 are the walk ways, and 2 is a flower
	int island[13][13]={{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
	{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
	{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
	{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
	{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
	{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
	{ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 },
	{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
	{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
	{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
	{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
	{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
	{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }};

	int randomNum;

	int count = 0;
	int stopper = 0;
	int x = 6;
	int y = 0;
	int cost=0;
	int averageCost;

	//variables to count how many times made it to the other bridge or rescued
	int madeIt = 0;
	int rescued = 0;

	for (count = 0; count < 5000;)
	{
		//resets the island, stopper, and x,y to original values
		int island[13][13] = { { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
		{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
		{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
		{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
		{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
		{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
		{ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 },
		{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
		{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
		{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
		{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
		{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
		{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } };

		 stopper = 0;

		 //starting point on the island
		 x = 6;
		 y = 0;


		do
		{

			randomNum = rand() % 100 + 1;

			if (randomNum <= 44)
			{
				//this is to step forward
				y += 1;

				if (island[x][y] == 2 || island[x][y] == 1)
				{
					island[x][y] -= 1;
					cost += 5;
					

				}
				else if (island[x][y] == -1)
				{
					stopper = -1;
					count++;
					rescued++;
				}
				else if (island[x][y] == 3)
				{
					stopper = 3;
					count++;
					madeIt++;

				}
			}
			else if (randomNum > 44 && randomNum <= 65)
			{
				//this steps right
				x += 1;

				if (island[x][y] == 2 || island[x][y] == 1)
				{
					island[x][y] -= 1;
					cost += 5;
				}
				else if (island[x][y] == -1)
				{
					stopper = -1;
					count++;
					rescued++;
				}
				else if (island[x][y] == 3)
				{
					stopper = 3;
					count++;
					madeIt++;
				}
			}
			else if (randomNum > 65 && randomNum <= 90)
			{
				//this steps left
				x -= 1;

				if (island[x][y] == 2 || island[x][y] == 1)
				{
					island[x][y] -= 1;
					cost += 5;
				}
				else if (island[x][y] == -1)
				{
					stopper = -1;
					count++;
					rescued++;
				}
				else if (island[x][y] == 3)
				{
					stopper = 3;
					count++;
					madeIt++;
				}
			}
			else if (randomNum > 90 && randomNum <= 100)
			{
				//this steps backwards
				y -= 1;

				if (island[x][y] == 2 || island[x][y] == 1)
				{
					island[x][y] -= 1;
					cost += 5;
				}
				else if (island[x][y] == -1)
				{
					stopper = -1;
					count++;
					rescued++;
				}
				else if (island[x][y] == 3)
				{
					stopper = 3;
					count++;
					madeIt++;
				}
			}
			else
			{
				cout << "error" << endl;
			}



		} while (stopper != 3 || stopper != -1);
		cout << stopper << endl;


	}

	averageCost = cost / 5000;

	cout << "Cost of walking on island" << averageCost << endl;
	cout << "Times rescued in water" << rescued << endl;
	cout << "Times made it to a bridge" << madeIt << endl;
		return 0;





	




}
I got it to work with the while(stopper !=3 && stopper != -1) Thank you for the feedback sometimes you just need someone to catch a few simple things to make it work.
Topic archived. No new replies allowed.