CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2008
    Posts
    1

    Unhappy Error I can't figure out in some simple C++ code, Please Help

    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.
    Someone Please Help.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Error I can't figure out in some simple C++ code, Please Help

    Quote Originally Posted by Kugelsicher88 View Post
    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.

  3. #3
    Join Date
    Feb 2005
    Location
    Madrid (Spain)
    Posts
    511

    Re: Error I can't figure out in some simple C++ code, Please Help

    Can you see any diference ? ....

    Code:
    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;
    }

    Please use code tags !!


    Regards.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured