# Vector subscript out of range? (Sudoku)

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-03-2009
mt1
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)
• 03-03-2009
matsp
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
• 03-03-2009
whiteflags
Well what do you think this does? vector's subscript operator does not take multiple indices.
• 03-03-2009
laserlight
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.
• 03-03-2009
mt1
Quote:

Originally Posted by matsp
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. :confused:
• 03-03-2009
mt1
Quote:

Originally Posted by laserlight
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.
• 03-03-2009
matsp
Quote:

Originally Posted by mt1
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. :confused:

Yes, so you may not need a vector of vectors, just an array of 81 elements.

--
Mats
• 03-03-2009
mt1
Quote:

Originally Posted by matsp
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.
• 03-03-2009
matsp
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
• 03-03-2009
mt1
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(); }```
• 03-03-2009
matsp
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
• 03-03-2009
mt1
Quote:

Originally Posted by matsp
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?
• 03-03-2009
anon
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.
• 03-03-2009
mt1
Quote:

Originally Posted by anon
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.
• 03-03-2009
matsp
Can you describe what it is that you are trying to tackle?

--
Mats
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last