• December 9th, 2008, 05:28 AM
Kugelsicher88
Here's the code for the function(also the global declarations used in the function are included), I marked with comments where the problem is, someone please help if you can:

// Global Declarations
int arrGameBrd[8][8];
int arrSolveBrd[8][8][8];
void ChangeOneValue();

void ChangeOneValue() {
// Declarations
int xCoor;
int yCoor;
char xyValue;
int intxyValue;
int count;

//BoardOut(0, 0); // I commented this to narrow down possible error location
cout << "Enter x coordinate(1-9) for the square to be changed." << endl;
cin >> xCoor;

if (xCoor < 1 || xCoor > 9) {
cout << "Invalid coordinate!" << endl;
return;

}

cout << "Enter y coordinate(1-9) for the square to be changed." << endl;
cin >> yCoor;

if (yCoor < 1 || yCoor > 9) {
cout << "Invalid coordinate!" << endl;
return;

}

//BoardOut(xCoor, yCoor); // I commented this to narrow down possible error location
cout << "Please enter the value for this square(1-9). Enter 0 to cancel." << endl;
cin.sync();
cin.get(xyValue);
cin.sync();
intxyValue = (xyValue - 48);

if (intxyValue < 0 || intxyValue > 9) {
cout << "Invalid value! Must be 1-9!" << endl;
return;

}
xCoor = (xCoor - 1);
yCoor = (yCoor - 1);
cout << xCoor << ", " << yCoor << endl; // added this line to find the problem, will refer to is as DBline0 below
cout << arrGameBrd[xCoor][yCoor] << ", " << arrGameBrd[1][0] << endl; // added this line to find the problem, will refer to it as DBline1 below
arrGameBrd[xCoor][yCoor] = intxyValue; // HERE'S THE PROBLEM LINE, see output below
cout << arrGameBrd[xCoor][yCoor] << ", " << arrGameBrd[1][0] << endl; // added this line to find the problem, will refer to it as DBline2 below
if (intxyValue != 0) {
for (count = 1; count < 10; count++) {
if (count == xyValue) {

}
else {
arrSolveBrd[xCoor][yCoor][(count - 1)] = 0;

}
}
}

return;
}

When I run the program the output I get from this function is:

Enter x coordinate(1-9) for the square to be changed.
1 // I press 1 then enter
Enter y coordinate(1-9) for the square to be changed.
9 // I press 9 then enter
Please enter the value for this square(1-9). Enter 0 to cancel.
3 // I press 3 then enter
0, 8 // This is the output from DBline0
0, 0 // This is the output from DBline1
3, 3 // This is the output from DBline2

Now, notice that this line:
arrGameBrd[xCoor][yCoor] = intxyValue;
somehow changes two values in the array [0][8] and [1][0]! AHHH, how is this happening I am just completely stumped, I know I'm a novice but it seems like an obvious impossibility.
• December 9th, 2008, 06:02 AM
Lindley
Quote:

Originally Posted by Kugelsicher88
int arrGameBrd[8][8];
...
Now, notice that this line:
arrGameBrd[xCoor][yCoor] = intxyValue;
somehow changes two values in the array [0][8] and [1][0]!

Since arrGameBrd is an 8x8 array, there *is* no [0][8] position. Doesn't exist----maximum index is 7. However, due to the way the array is laid out in memory when you define it this way, moving one int past arrGameBrd[0][7] puts you at arrGameBrd[1][0]. Hence why you're seeing the effects you are.
• December 9th, 2008, 03:07 PM
juanpast
```int arrGameBrd[8][8]; int arrSolveBrd[8][8][8]; void ChangeOneValue(); void ChangeOneValue() {   // Declarations   int xCoor;   int yCoor;   char xyValue;   int intxyValue;   int count;   //BoardOut(0, 0); // I commented this to narrow down possible error location   cout << "Enter x coordinate(1-9) for the square to be changed." << endl;   cin >> xCoor;   if (xCoor < 1 || xCoor > 9) {     cout << "Invalid coordinate!" << endl;     return;   }   cout << "Enter y coordinate(1-9) for the square to be changed." << endl;   cin >> yCoor;   if (yCoor < 1 || yCoor > 9) {     cout << "Invalid coordinate!" << endl;     return;   }   //BoardOut(xCoor, yCoor); // I commented this to narrow down possible error location   cout << "Please enter the value for this square(1-9). Enter 0 to cancel." << endl;   cin.sync();   cin.get(xyValue);   cin.sync();   intxyValue = (xyValue - 48);   if (intxyValue < 0 || intxyValue > 9) {     cout << "Invalid value! Must be 1-9!" << endl;     return;   }   xCoor = (xCoor - 1);   yCoor = (yCoor - 1);   cout << xCoor << ", " << yCoor << endl; // added this line to find the problem, will refer to is as DBline0 below   cout << arrGameBrd[xCoor][yCoor] << ", " << arrGameBrd[1][0] << endl; // added this line to find the problem, will refer to it as DBline1 below   arrGameBrd[xCoor][yCoor] = intxyValue; // HERE'S THE PROBLEM LINE, see output below   cout << arrGameBrd[xCoor][yCoor] << ", " << arrGameBrd[1][0] << endl; // added this line to find the problem, will refer to it as DBline2 below   if (intxyValue != 0) {     for (count = 1; count < 10; count++) {       if (count == xyValue) {       }       else {         arrSolveBrd[xCoor][yCoor][(count - 1)] = 0;       }     }   }   return; }```