我有(又一个)关于字符的问题。感谢之前帮助过我的人。在程序的这一点上,我主要尝试做 4 件事。即:
构建一个 9x9 的二维数组并用下划线填充它。
询问行/列,然后询问用户希望进入该行/列的次数不限次数。
用指定的数字替换指定的空格。
在 ASCII 艺术数独板上输出整个 9x9 字符数组。
(稍后解决。)
我的问题是,当我输入行/列和我想进入该行/列的数字时,原来在该位置的破折号消失了,但我输入的数字没有出现在它的位置。
这是目前的代码:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main () {
//Builds 9x9 char array.
char dash[9][9];
for (int array=0; array<9; array++) {
for (int array2=0; array2<9; array2++) {
dash[array][array2]='_';
}
}
cout << "Input the row #, then the column #, then the number that you wish to fill that spot." << endl;
cout << "Remember that a Sudoku board is 9x9." << endl;
cout << "When you wish to finish input and solve, type all 0's and press enter." << endl;
int rowb;
char row[99];
int columnb;
char column[99];
int numb;
char num[99];
//Inputs the row/column and number to go into specified row/column.
int control=0;
while (rowb!=0){
control++;
cout << "Row: ";
cin >> rowb;
cout << "Column: ";
cin >> columnb;
cout << "Number: ";
cin >> numb;
row[control]=rowb-1;
column[control]=columnb-1;
num[control]=numb;
}
int length;
length=strlen(row);
//Replaces the _'s in the specified rows/columns and replaces them with the integer the user specified. This is where I think I'm having the problem.
for (control=0; control<length; control++) {
dash[row[control]][column[control]]=num[control];
}
//Builds the Sudoko board and outputs the full 9x9 array.
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=3; count<6; 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=6; count<9; 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;
}
最佳答案
循环中输入的数字赋值有问题。
//Replaces the _'s in the specified rows/columns and replaces them with the integer the user specified. This is where I think I'm having the problem.
for (control=0; control<length; control++) {
dash[row[control]][column[control]]=num[control]; //<<<--- Assignment issue.
}
您在字符数组中分配一个整数值,因此当您显示时,您将获得 ascii 值的相应字符,而不是整数。尝试按如下方式更改分配:
//Replaces the _'s in the specified rows/columns and replaces them with the integer the user specified. This is where I think I'm having the problem.
for (control=0; control<length; control++) {
dash[row[control]][column[control]]=num[control] + '0'; // Convert to ascii value of the integer, but will fail if not b/w 0 & 9.
}
如果您选择使用上述观察,还建议检查输入的数字是否在 1 和 9 之间。
如果输入的值不是黑白 1 和 9,请添加对作为输入值输入的行和列的检查,如果输入的值不是黑白 1 和 9,将导致未定义的行为,因为访问超出绑定(bind)的数组元素。
同样如本杰明林德利所提到的,请更新 strlen
代码。
希望这对您有所帮助!
关于c++ - 数独游戏设计问题C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8697028/