抱歉,如果这没有意义,但我正在为数独游戏写一个程序。它获取一个文件,将其转换为矩阵,并在屏幕上打印电路板。那么用户应该能够编辑游戏。我的问题是我的编辑功能。每当我试图在电路板上编辑一个值时,它就会去掉那个空间。
void edit(char sudoku[][9])
{
char letter;
int number;
//these are the coordinates for the board
int value = 0;
//this is the entered value for the choosen square
cout << "What are the coordinates of the square: ";
cin >> letter >> number;
letter = toupper(letter); // makes sure the letter is caps
if (sudoku[letter - 65][number - 1] != ' ')
// if the coordinates are off the board or already have a value
{
cout << "Error: Square \'" << letter << number
<< "\' is invalid."
<< endl;
}
else
{
cout << "What is the value at \'" << letter << number
<< "\': ";
cin >> value;
if (value > 9 || value < 1)
//if the value is invalid
{
cout << "ERROR: Value \'" << value
<< "\'in square \'" << letter << number
<< "\' is invalid\n";
}
cout << endl;
sudoku[letter - 65][number - 1] = value;
//set the square = the entered value
}
return;这是编辑前的电路板:
A B C D E F G H I
1 7 2 3| |1 5 9
2 6 |3 2| 8
3 8 | 1 | 2
-----+-----+-----
4 7 |6 5 4| 2
5 4|2 7|3
6 5 |9 3 1| 4
-----+-----+-----
7 5 | 7 | 3
8 4 |1 3| 6
9 9 3 2| |7 1 4在此之后:
What are the coordinates of the square: b2
What is the value at 'B2': 3
A B C D E F G H I
1 7 2 3| |1 5 9
2 6 |3 2| 8
3 8 | 1 | 2
-----+-----+-----
4 7 |6 5 4| 2
5 4|2 7|3
6 5 |9 3 1| 4
-----+-----+-----
7 5 | 7 | 3
8 4 |1 3| 6
9 9 3 2| |7 1 4所以唯一的变化是在编辑后删除了一个空格。
发布于 2019-12-17 03:51:26
这一行:
sudoku[letter - 65][number - 1] = value;将ASCII值0-9放入由char组成的sudoku数组中。这些ASCII值通常是不可见的,或者具有其他特殊含义,如蜂鸣声或制表。
您需要将'0'的值添加到它上面,才能使它正常显示:
sudoku[letter - 'A'][number - 1] = value + '0';并将像65这样的魔术数字替换为字符文字,比如'A'。
https://stackoverflow.com/questions/59363328
复制相似问题