1

I'm new to C++, only been programming a few days so this might seem stupid, but can you spot why my arrays are not working correctly? This is the start of a program I'm designing that will solve Sudoku puzzles, but the 2D array that I'm using to solve it isn't working correctly.

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

int main () {
    char dash[9][9];
    for (int array=0; array<9; array++) {
        for (int array2=0; array2<9; array2++) {
            dash[array][array2]=array2;
            cout << dash[array][array2];
        }
    }
    cout << dash[1][4] << endl; //This is temporary, but for some reason nothing outputs when I do this command.
    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=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=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;
return 0;

}

Also I'm aware that there may be easier ways to build the Sudoku board, but I can already see in my mind how this one will work, and if it fails, well, the only way to learn is by failure. All I want to know is what is wrong with the arrays.

menjaraz
  • 7,551
  • 4
  • 41
  • 81
  • @Ben The chars won't output at all. I even added the extra COUT as you can see. I told it to output a random array value and it didn't even do that. –  Jan 01 '12 at 05:54
  • The code seems to compile just fine... http://codepad.org/tKPRn9rZ What's the problem? – Cody Gray - on strike Jan 01 '12 at 05:55
  • @CodyGray This is what he expected :) http://codepad.org/kUqg1yOd – COD3BOY Jan 01 '12 at 05:59
  • right but you see how it's all weird like that? there is supposed to be numbers in each box, 1-9, on each row. @Ben fixed my problem. –  Jan 01 '12 at 06:00

3 Answers3

5

You've got numeric data stored in your char array, which is fine, but cout tries to print it as a character. Try casting to integer during output:

cout << (int)dash[count][count2*3]

The other option is to store characters in the array:

for (int array=0; array<9; array++) {
    for (int array2=0; array2<9; array2++) {
        dash[array][array2] = '0' + array2;
    }
}
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
3

You're trying to display chars as if they were integers. Well, technically, they are, but they don't display as integers. Either change your char array to an int array(very easy), or every time you display the data, cast it to int(tedious).

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
0

Change char dash[9][9] to int dash[9][9]. You assign small numbers to dash[i][j], as chars they are mostly unprintable control characters, so nothing intelligible gets printed. As ints they are printed as you expect.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
Software_Designer
  • 8,490
  • 3
  • 24
  • 28