我还有(另一个)关于查斯的问题。感谢那些曾经帮助过我的人。在这个时候,我主要做4件事情。这就是:
underscores.
(解决办法将在稍后进行。)
我的问题是,当我输入行/列和我想进入该行/列的数字时,原来在那个位置的破折号消失了,但我输入的数字没有出现在它的位置上。
以下是目前为止的代码:
#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;
}发布于 2012-01-02 03:17:10
在循环中输入的数字有一个问题。
//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之间。
如果输入的值不是b/w1和9,则请添加对输入值的行和列的检查,如果输入的值不是b/w1和9,则如果输入的值不是b/w1和9,则由于访问绑定数组元素而导致未定义行为。
另外,正如本杰明林德利提到的,请更新strlen代码。
希望这能有所帮助!
发布于 2012-01-02 03:07:39
length=strlen(row);这是未定义的行为,因为行从来没有初始化过,而且您也从来没有空过终止字符串。
char row[99];
...
int control=0;
while (rowb!=0){
control++;
...
row[control]=rowb-1;
...注意,第一次通过循环时,控件是1。所以,您设置的是row1的值,而不是row。将增量移到循环的末尾。可能还有其他一些问题,但这是你所看到的行为的主要原因。
此外,要使strlen工作,您需要空终止字符串。
最后,您正在犯与this question和this question相同的错误。你为什么不看上去明白?字符显示与ints不同。以下代码将不显示数字1:
char c = 1;
std::cout << c;看看另外两个问题的答案。
https://stackoverflow.com/questions/8697028
复制相似问题