Thread: Vector subscript out of range? (Sudoku)

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    11

    Thumbs down Sudoku help

    Hi guys, please bare with me, i'm a bit of a newbie to C++. I'm trying to write a Sudoku puzzle. I'm trying to get the program to ask the user a number between 0-80 (which represents the board position) so that the user can input a number 1-9 in to that place.

    My code is probably too long, probably is a shorter way of doing it. Here's my code.

    [fixed] The problem: it says the "Vector subscript out of range", i presume the problem is where i've put the code in bold (look) below. How can I fix this?

    (look at post #10 for my code)
    Last edited by mt1; 03-03-2009 at 07:38 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You have a vector with 9 elements, numbered from 0..8 - you probably want a vector of vector.

    I'm not sure what you expect this to do:
    Code:
    cout << "\n\t" << board[0, 1, 2] << "      |" << board[3, 4, 5] << "        | " << board[6, 7, 8];
    But I expect it is not doing what you expect, as what it is actually being done is:
    Code:
    cout << "\n\t" << board[2] << "      |" << board[5] << "        | " << board[8];
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Well what do you think this does? vector's subscript operator does not take multiple indices.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Based on this:
    Code:
    const int NUM_SQUARES = 9;
    vector<char> board(NUM_SQUARES, EMPTY);
    It looks like your vector has a size of 9.

    Now, board[0, 1, 2] is actually board[2]. The comma operator is effect, thus 0, 1, 2 is evaluated as 2. Once we go to board[9, 10, 11], we see that we are accessing board[11], which is out of bounds. Actually, even board[9] is out of bounds. You probably want to print board[0] to board[8] in turn.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    Quote Originally Posted by matsp View Post
    You have a vector with 9 elements, numbered from 0..8 - you probably want a vector of vector.

    I'm not sure what you expect this to do:
    Code:
    cout << "\n\t" << board[0, 1, 2] << "      |" << board[3, 4, 5] << "        | " << board[6, 7, 8];
    But I expect it is not doing what you expect, as what it is actually being done is:
    Code:
    cout << "\n\t" << board[2] << "      |" << board[5] << "        | " << board[8];
    --
    Mats
    Well the number 0-80 represents the cell location, the program asks the user to input say cell location 15, then a number 1-9.

    To be honest i don't really know how to tackle this.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    Quote Originally Posted by laserlight View Post
    Based on this:
    Code:
    const int NUM_SQUARES = 9;
    vector<char> board(NUM_SQUARES, EMPTY);
    It looks like your vector has a size of 9.

    Now, board[0, 1, 2] is actually board[2]. The comma operator is effect, thus 0, 1, 2 is evaluated as 2. Once we go to board[9, 10, 11], we see that we are accessing board[11], which is out of bounds. Actually, even board[9] is out of bounds. You probably want to print board[0] to board[8] in turn.

    Ahh.. ok, i'll go try it again, thanks.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mt1 View Post
    Well the number 0-80 represents the cell location, the program asks the user to input say cell location 15, then a number 1-9.

    To be honest i don't really know how to tackle this.
    Yes, so you may not need a vector of vectors, just an array of 81 elements.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    Quote Originally Posted by matsp View Post
    Yes, so you may not need a vector of vectors, just an array of 81 elements.

    --
    Mats

    Yep, I got it now, just going to take me a while lmao. Thanks all.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, if you try to control your laughter, you may get there a bit quicker!

    And remember, in a year or two, you'll think "and why did I think that was so hard..."?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    Is there not another way to do this?
    Code:
    // Sudoku
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <windows.h>
    
    using namespace std;
    
    // function prototypes
    void instructions();
    void displayBoard(const vector<char>& board);
    
    // main function
    int main()
    {
        const int NUM_SQUARES = 81;
        vector<char> board(NUM_SQUARES);
    	
        instructions();
    Sleep(2000);
        displayBoard(board);
    
        return 0;
    }
    
    // functions
    void instructions()
    {
        cout << "Welcome to Sudoku!" << endl << endl;
        cout << "Make your move known by entering a number, 0 - 80." << endl;
        cout << "The number corresponds to the desired board position, as illustrated: " << endl; 
    	cout <<  endl;
        
        cout << "       0  1  2 | 3  4  5 | 6  7  8 |\n";
        cout << "       9  10 11| 12 13 14| 15 16 17|\n";
        cout << "       18 19 20| 21 22 23| 24 25 26|\n";
        cout << "       -----------------------------\n";
        cout << "       27 28 29| 30 31 32| 33 34 35|\n";
        cout << "       36 37 38| 39 40 41| 42 43 44|\n";
        cout << "       45 46 47| 48 49 50| 51 52 53|\n";
        cout << "       -----------------------------\n";
        cout << "       54 55 56| 57 58 59| 60 61 62|\n";
        cout << "       63 64 65| 66 67 68| 69 70 71|\n";
        cout << "       72 73 74| 75 76 77| 78 79 80|\n\n";
    
        cout << "Here comes the puzzle.\n\n";
    }
    
    
    void displayBoard(const vector<char>& board)
    {
    cout << "\n\t" << board[0] << "  5" << "  1" << "| 7" << " " << board[4] << " " << board[5] << "   | " << board[6] << "  6" << "  2";
    cout << "\n\t9"<< " " << board[10] << " " << board[11] << "  | " << board[12] << " " << board[13] << "   6" << " | " << board[15] << " " << board[16] << "   8" << board[17];
    cout << "\n\t8"<< "  3" << " " << board[20] << " | " << board[21] << " " << board[22] << "   2" << " | " << "1";
    cout << "\n\t" << "---------------------------";
    
    	cin.get();
    }
    Last edited by mt1; 03-03-2009 at 07:09 AM.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You mean setting up the "constant" (already filled in) values?

    You could just put them into your vector before you show the board the first time.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    Quote Originally Posted by matsp View Post
    You mean setting up the "constant" (already filled in) values?

    You could just put them into your vector before you show the board the first time.

    --
    Mats
    Yes.

    Don't understand?

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Eventually you'll need to start using loops (and even nested loops), e.g to display the contents of the vector.

    And aside, your sleep function is terrible. On Windows include "windows.h" and just use the Sleep function. Linux should have a corresponding function sleep. There is no point in heating the processor to do nothing useful.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  14. #14
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    Quote Originally Posted by anon View Post
    Eventually you'll need to start using loops (and even nested loops), e.g to display the contents of the vector.

    And aside, your sleep function is terrible. On Windows include "windows.h" and just use the Sleep function. Linux should have a corresponding function sleep. There is no point in heating the processor to do nothing useful.
    Changed thanks. I'm still not too sure how to tackle this.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can you describe what it is that you are trying to tackle?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overflow and range checking for mul/div
    By Elysia in forum C++ Programming
    Replies: 28
    Last Post: 06-06-2008, 02:09 PM
  2. Help for a sudoku Solver
    By axilleask in forum C Programming
    Replies: 3
    Last Post: 11-26-2007, 04:28 PM
  3. Srand () w/ range
    By xsbinary in forum C Programming
    Replies: 9
    Last Post: 10-21-2007, 03:24 PM
  4. Please help to check.
    By nicoleha in forum C Programming
    Replies: 16
    Last Post: 12-07-2005, 03:29 PM
  5. Sudoku - the new addiction
    By axon in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-07-2005, 11:39 PM