1

我是 C++ 新手,只编程了几天,所以这可能看起来很愚蠢,但你能发现为什么我的数组不能正常工作吗?这是我正在设计的解决数独难题的程序的开始,但我用来解决它的二维数组无法正常工作。

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

另一种选择是将字符存储在数组中:

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]。您将小数字分配给s dash[i][j],因为char它们大多是不可打印的控制字符,因此不会打印任何可理解的内容。正如int您所期望的那样打印它们。

于 2012-01-01T08:05:28.643 回答