1

私は C++ の初心者で、プログラミングを始めてまだ数日なので、ばかげているように思えるかもしれませんが、私の配列が正しく動作しない理由を見つけていただけますか? これは、数独パズルを解くために設計しているプログラムの始まりですが、それを解くために使用している 2D 配列が正しく機能していません。

#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;

}

また、数独ボードをもっと簡単に組み立てる方法があるかもしれないことは承知していますが、これがどのように機能するかはすでに頭の中にあります。失敗した場合、学習する唯一の方法は失敗することです。私が知りたいのは、配列の何が問題なのかということだけです。

4

3 に答える 3

5

char配列に格納されている数値データは問題ありませんがcout、文字として出力しようとしています。出力中に整数にキャストしてみてください。

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

もう 1 つのオプションは、配列に文字を格納することです。

for (int array=0; array<9; array++) {
    for (int array2=0; array2<9; array2++) {
        dash[array][array2] = '0' + array2;
    }
}
于 2012-01-01T05:53:41.257 に答える
3

文字を整数のように表示しようとしています。技術的にはそうですが、整数として表示されません。char 配列を int 配列に変更する (非常に簡単) か、データを表示するたびに int にキャストします (面倒です)。

于 2012-01-01T05:54:32.440 に答える
0

に変更 char dash[9][9]int dash[9][9]ます。ほとんどが印刷できない制御文字であるためdash[i][j]、小さい数字を に割り当てます。したがって、理解できるものは何も印刷されません。期待どおりに印刷されますcharint

于 2012-01-01T08:05:28.643 に答える