gpt4 book ai didi

c++ - 这个char数组代码有什么问题?

转载 作者:太空宇宙 更新时间:2023-11-03 10:33:46 26 4
gpt4 key购买 nike

我是 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;

我还知道可能有更简单的方法来构建数独板,但我已经在脑海中看到了这个板的工作原理,如果它失败了,唯一的学习方法就是失败。我只想知道数组出了什么问题。

最佳答案

您已经将数字数据存储在 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;
}
}

关于c++ - 这个char数组代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8692011/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com