Entering Coordinates to an Array

I am working on an extra credit project for a class to build a Conway's game of Life.
I am trying to build a function for entering coordinates, and have been getting a Windows Error when it executes. I found another version on these forums, and that seems to work. But I'm wondering why my original doesn't. Any help would be appreciated.

Here is my original code that gives the error. The error pops up as soon as I've entered a Value for the X, and the screen displays the "Y" for me to enter.

while (x != -1)
{
cout << "X" << endl;
cin >> x;
if (x == -1)
break;
cout << "Y" << endl;
show[y-1][x-1] = 1;
}

And here is the version I found that seems to work.

while (x != -1)
{
cin >> x;
if (x == -1)
break;
cin >> y;
show[y-1][x-1] = 1;
}

To me, it looks as if the only difference is that I am trying to indicate what coordinate the users is entering, But why does adding those cout statements cause the error? I'm sure it's something stupid and obvious that I am just missing.

Thanks
Last edited on
You're not asking the user to enter y in your code (no cin >> y) therefore y is probably undefined and is equal to some huge value that goes out of the bounds for this array.
As I said, it's probably something really stupid and obvious.
Thanks for pointing that out to me.
Okay, I suspect (or at least hope) that this will be jsut as obvious as my previous question.

All of the functions seem to be working correctly now. In the Main (), I have a loop based on the number of iterations the user enters. The first time through the loop, the Caluclate and Switch functions work fine. But the 2nd time through the loop, the Calculate function doesn't seem to be doing anything. I have added a number of Display() to my loop so that I can see what Show and Work look like at each step. Show does have the new array the 2nd time through, but after Calculate runs, Work is not any different, and I would expect it to be. It's as if Show isn't being passed to Calculate() the second time.

Again, this is probably as simple as the last issue I had was, and I just can't see it because I've been staring at it for a while.

Here's the whole code:

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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

const int h = 20;
const int w = 70;
bool show [h] [w], work [h] [w];
int x, y, neighbors, iterations, c, row, col;


//Functions

// Instructions() - describes the game and input
void Instructions()
{
     cout << "Welcome to Conway's Game of Life\n\n\n"
          << "The game is played on a 70 x 20 space grid.\n"
          << "Empty spaces are 'dead' and 1s represent 'life'.\n\n"
          << "There are 3 simple rules:\n"
          << "     If a space is dead, with exactly 3 live neighbors, "
          << "that space\nwill be 'born'.\n"
          << "     If a space is alive, with 2 or 3 live neighbors, "
          << "that space\nwill continue to live.\n"
          << "     If a space is alive with 0, 1, or 4 or more dead "
          << "neighbors, that\nspace will die.\n\n"
          << "Enter an X (1-70) and Y (1-20) coordinate where you would "
          << "like to seed\nLife, with 0, 0 the upper left corner.\n"
          << "To remove life, enter the same coordinate a second time.\n"
          << "Type '-1' as an X to indicate you are done entering and wish "
          << "to start\nthe simulation" <<endl;
}

//Clear() - Clears the arrays
void Clear(bool matrix[h][w])
{
      for(row=0; row<h; row++)
      {
               for(col=0; col<w; col++)
               matrix[row][col] = 0;
      }    
}     

//Display() - prints an Array
void Display(bool matrix[h][w])
{
     for(row=0; row<h; row++)
     {
                for(col=0; col<w; col++)
                {
                           if (col==0)
                           cout << endl << matrix[row][col];
                           else
                           cout << matrix[row][col];
                }
     }
     cout << endl << endl;
}


//InvalidC() - displays an invalid entry message for columns
void InvalidC()
{
     cout << "That is an invalid selection.\n"
          << "Please enter a value between 1 and 70\n"
          << "X  ";
          cin >> x;
}

//InvalidR() - displays an invalid entry message for rows
void InvalidR()
{
     cout << "That is an invalid selection.\n"
          << "Please enter a value between 1 and 20\n"
          << "Y  ";
          cin >> y;
}


//Input() - receives input and populates an array
void Input(bool matrix [h][w])
{
     while (x != -1)
     {
           cout << "X  ";
           cin >> x;
           if (x == -1)
           break;
           while (x < 0 || x > w)
           InvalidC();
           cout << "Y  ";
           cin >> y;
           while (y < 0 || y > h)
           InvalidR();
           if (show[y-1][x-1] == 0)
           show[y-1][x-1] = 1;
           else
           matrix[y-1][x-1] = 0;
           Display (show);
     }

    cout << endl;
    cout << "Enter the number of 'ticks' that you want the\n"
         << "simulation to run" << endl;
         cin >> iterations;
}

//Swtich() - moves array Work into Array Show
void Switch(bool matrixa[h][w], bool matrixb[h][w])
{
    for(row=0; row<h; row++)
     {
                for(col=0; col<w; col++)
                {
                           matrixa[row][col] = matrixb[row][col];
                }
     }
}           
           
//Calculate() - Counts neighbors, and calculates the new array
void Calculate(bool matrixa[h][w], bool matrixb[h][w])
{
     for(row=0; row<h; row++)
     {
                for(col=0; col<w; col++)
                {
                           neighbors=0;
                           // count neighbors
                           if (matrixa [row-1][col-1] == 1) neighbors +=1;
                           if (matrixa [row-1][col] == 1) neighbors +=1;
                           if (matrixa [row-1][col+1] == 1) neighbors +=1;
                           if (matrixa [row][col-1] == 1) neighbors +=1;
                           if (matrixa [row][col+1] == 1) neighbors +=1;
                           if (matrixa [row+1][col-1] == 1) neighbors +=1;
                           if (matrixa [row+1][col] == 1) neighbors +=1;
                           if (matrixa [row+1][col+1] == 1) neighbors +=1;
                           
                           //calculate life
                           if (matrixa [row][col] == 1 && neighbors < 2)
                           matrixb [row][col] = 0;
                           else if (matrixa [row][col] == 1 && neighbors > 3)
                           matrixb [row][col] = 0;
                           else if (matrixa [row][col] == 1 && 
                           (neighbors == 2 || neighbors == 3))
                           matrixb [row][col] = 1;
                           else if (matrixa [row][col] == 0 && neighbors == 3)
                           matrixb [row][col] = 1;
                }
     }                    
}

// declaration of main program

int main()

{
Instructions();
Clear(show);
Clear(work);
Input(show);
Display(show);
for (c=0; c<iterations; c++)
{   
    cout << "show1";
    Display(show);
    Clear(work);
    cout << "work1";
    Display(work);
    Calculate(show, work);
    cout << "show2";
    Display(show);
    cout << "work2";
    Display(work);
    Switch(show, work);
    cout << "show3";
    Display(show);
    cout << "work3";
    Display(work);
}
}
As I expected, I think it was somehting really, really stupid. The set of 'life' that I was seeding with for testing went into a state for the second iteration where there would be no further changes. But I just tested with a known glider and it seemed to move across the array as I would expect it to. Now I just need to figure out how to slow the simulation down so that you can actually see what it's doing. But I'll play with that for a while before asking about it.
Topic archived. No new replies allowed.