首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编辑矩阵中的值就是删除它

编辑矩阵中的值就是删除它
EN

Stack Overflow用户
提问于 2019-12-17 03:46:39
回答 1查看 65关注 0票数 0

抱歉,如果这没有意义,但我正在为数独游戏写一个程序。它获取一个文件,将其转换为矩阵,并在屏幕上打印电路板。那么用户应该能够编辑游戏。我的问题是我的编辑功能。每当我试图在电路板上编辑一个值时,它就会去掉那个空间。

代码语言:javascript
复制
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;

这是编辑前的电路板:

代码语言:javascript
复制
   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

在此之后:

代码语言:javascript
复制
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

所以唯一的变化是在编辑后删除了一个空格。

EN

回答 1

Stack Overflow用户

发布于 2019-12-17 03:51:26

这一行:

代码语言:javascript
复制
sudoku[letter - 65][number - 1] = value;

将ASCII值0-9放入由char组成的sudoku数组中。这些ASCII值通常是不可见的,或者具有其他特殊含义,如蜂鸣声或制表。

您需要将'0'的值添加到它上面,才能使它正常显示:

代码语言:javascript
复制
sudoku[letter - 'A'][number - 1] = value + '0';

并将像65这样的魔术数字替换为字符文字,比如'A'

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59363328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档