1

I have (yet another) question about chars. Thanks to those who helped me with this before. I'm trying to do mainly 4 things at this point in the program. That is:

  1. Build a 2D array 9x9 and fill it with underscores.

  2. Ask for a row/column and then the number that the user wishes to go into that row/column as many times as the user wants.

  3. Replace the specified blanks with the specified numbers.

  4. Output the entire 9x9 char array on an ASCII art Sudoku board.

(Solving will come later.)

My problem is that when I enter the row/column and the number that I want to go into that row/column the dash that was originally in that spot disappears, but the number I entered does not appear in its place.

Here is the code so far:

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main () {

//Builds 9x9 char array.
char dash[9][9];
for (int array=0; array<9; array++) {
    for (int array2=0; array2<9; array2++) {
        dash[array][array2]='_';
    }
}

cout << "Input the row #, then the column #, then the number that you wish to fill that spot." << endl;
cout << "Remember that a Sudoku board is 9x9." << endl;
cout << "When you wish to finish input and solve, type all 0's and press enter." << endl;

int rowb;
char row[99];
int columnb;
char column[99];
int numb;
char num[99];

//Inputs the row/column and number to go into specified row/column.
int control=0;
while (rowb!=0){
    control++;
    cout << "Row: ";
    cin >> rowb;
    cout << "Column: ";
    cin >> columnb;
    cout << "Number: ";
    cin >> numb;
    row[control]=rowb-1;
    column[control]=columnb-1;
    num[control]=numb;
}

int length;
length=strlen(row);
//Replaces the _'s in the specified rows/columns and replaces them with the integer the user specified. This is where I think I'm having the problem.
for (control=0; control<length; control++) {
    dash[row[control]][column[control]]=num[control];
}

//Builds the Sudoko board and outputs the full 9x9 array.
cout << "╔═══════════╦═══════════╦═══════════╗" << endl;
for (int count=0; count<3; count++) {
    for (int count2=0; count2<3; count2++) {
        cout << "║_" << dash[count][count2*3] << "_|_" << dash[count][count2*3+1] << "_|_" << dash[count][count2*3+2] << "_";   
    }
        cout << "║" << endl;
}
cout << "╠═══════════╬═══════════╬═══════════╣" << endl;
for (int count=3; count<6; count++) {
    for (int count2=0; count2<3; count2++) {
        cout << "║_" << dash[count][count2*3] << "_|_" << dash[count][count2*3+1] << "_|_" << dash[count][count2*3+2] << "_";   
    }
    cout << "║" << endl;
}
cout << "╠═══════════╬═══════════╬═══════════╣" << endl;
for (int count=6; count<9; count++) {
    for (int count2=0; count2<3; count2++) {
        cout << "║_" << dash[count][count2*3] << "_|_" << dash[count][count2*3+1] << "_|_" << dash[count][count2*3+2] << "_";   
    }
    cout << "║" << endl;
}
cout << "╚═══════════╩═══════════╩═══════════╝" << endl;
return 0;
}
Jon Purdy
  • 53,300
  • 8
  • 96
  • 166

2 Answers2

1

There is a problem assignment of the number entered in the loop.

//Replaces the _'s in the specified rows/columns and replaces them with the integer the user specified. This is where I think I'm having the problem.
for (control=0; control<length; control++) {
    dash[row[control]][column[control]]=num[control]; //<<<--- Assignment issue.
}

You are assigning an integer value in a character array & thus when you display you will get the corresponding char for the ascii value & not the integer. Try changing the assignment as follows:

//Replaces the _'s in the specified rows/columns and replaces them with the integer the user specified. This is where I think I'm having the problem.
for (control=0; control<length; control++) {
    dash[row[control]][column[control]]=num[control] + '0'; // Convert to ascii value of the integer, but will fail if not b/w 0 & 9.
}

Checking if the number entered in is between 1 & 9 is also advised if you choose to use the above observation.
Please add checks for the row & column entered as enter values which are not b/w 1 & 9 will lead to undefined behaviour due to accessing out of bound array elements if the values entered are not b/w 1 & 9.
Also as mentioned by Benjamin Lindley please update strlen code.
Hope this helps!

another.anon.coward
  • 11,087
  • 1
  • 32
  • 38
0
length=strlen(row);

This is undefined behavior, because row[0] was never initialized, and you never null terminate the string.

char row[99];
...
int control=0;
while (rowb!=0){
    control++;
    ...
    row[control]=rowb-1;
    ...

Notice that the first time through the loop, control is 1. So, you're setting the value of row[1], but not row[0]. Move the increment to the end of the loop. There may be some other problems, but this is the primary one responsible for the behavior you're seeing.

Also, for strlen to work, you need to null terminate the string.

And finally, you're making the same mistake you've made in this question and this question. Why aren't you seeming to get that? Chars display differently than ints. The following code will not display the number 1:

char c = 1;
std::cout << c;

Look at the answers to those other two questions.

Community
  • 1
  • 1
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • I did that but still didn't get the desired output. But thanks for that info. –  Jan 02 '12 at 03:12
  • I know. I've been looking around the internet but without a good book I really don't have a true understanding of how chars work. Thanks for helping me in my ignorance, though. –  Jan 02 '12 at 03:23
  • 2
    @Redmastif: Then get a book. [Here's a list of good ones.](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) If you have no money, [one of those you can get for free online.](http://www.ibiblio.org/pub/docs/books/eckel/) – Benjamin Lindley Jan 02 '12 at 03:32