这个字符数组代码有什么问题?
我是 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;
另外,
我知道可能有更简单的方法来构建数独板,但我已经在脑海中看到了这个方法将如何工作,如果它失败了,那么,唯一的学习方法就是通过失败。我只想知道数组出了什么问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您已将数字数据存储在
char
数组中,这很好,但cout
尝试将其打印为字符。尝试在输出期间转换为整数:另一个选项是将字符存储在数组中:
You've got numeric data stored in your
char
array, which is fine, butcout
tries to print it as a character. Try casting to integer during output:The other option is to store characters in the array:
您试图将字符显示为整数。嗯,从技术上讲,它们是,但它们不显示为整数。要么将 char 数组更改为 int 数组(非常简单),要么每次显示数据时将其转换为 int(乏味)。
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).
将
char dash[9][9]
更改为int dash[9][9]
。您可以为dash[i][j]
分配较小的数字,因为char
它们大多是不可打印的控制字符,因此不会打印任何可理解的内容。作为int
,它们会按照您的预期打印。Change
char dash[9][9]
toint dash[9][9]
. You assign small numbers todash[i][j]
, aschar
s they are mostly unprintable control characters, so nothing intelligible gets printed. Asint
s they are printed as you expect.