Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;2D字符数组有时在随机位置复制数据_C++_Arrays_Char_2d - Fatal编程技术网

C++ c++;2D字符数组有时在随机位置复制数据

C++ c++;2D字符数组有时在随机位置复制数据,c++,arrays,char,2d,C++,Arrays,Char,2d,我使用2D字符数组在屏幕上显示一块板。空板上满是“.”(点),程序在某个位置写入另一个字符(“G”)。问题是有时(每四次多多少少出现一次)一个重复字符出现在另一个位置 4 |. . . . . . | 3 |. . . . . . | 2 |G . . . . . | 1 |. . . . G . | ------------------------ a b c d e f 相关代码片段包括: class Field {

我使用2D字符数组在屏幕上显示一块板。空板上满是“.”(点),程序在某个位置写入另一个字符(“G”)。问题是有时(每四次多多少少出现一次)一个重复字符出现在另一个位置

 4 |.  .  .  .  .  .  |
 3 |.  .  .  .  .  .  |
 2 |G  .  .  .  .  .  |
 1 |.  .  .  .  G  .  |
------------------------
    a  b  c  d  e  f
相关代码片段包括:

class Field
{
 public:
static const int FIELDWIDTH=6;
static const int FIELDHEIGHT=4;

char field[FIELDWIDTH][FIELDHEIGHT];
..
};

void Field::empty(void){
  for(int r=0; r<FIELDHEIGHT; r++)
    for(int c=0; c<FIELDWIDTH; c++)
      field[r][c] = '.';
};

Field::plot(){                   //Plots the board
for(int r=FIELDHEIGHT;r>0;r--){
    cout << std::setw(2) << std::setfill(' ') << r << " |";
    for(int c=0;c<FIELDWIDTH;c++)
      cout << field[r-1][c] << "  ";
    cout << "|" << endl;
  };
...
};

void Field::putChar(void){
...
(random definition of pr2 and pc2 and validation within limits)
...
 field[pr2][pc2]='G';
 printCors(pr2,pc2); //cout coordinate for debug
...
};
putChar中的赋值由cout监视,cout仅输出一个坐标,但有时会出现一个附加字符,与预期字符有一定的关系,这里有一些运行:

b1=
 4 |.  .  .  .  .  .  |
 3 |.  .  .  .  .  .  |
 2 |.  .  .  .  .  .  |
 1 |.  G  .  .  .  .  |
------------------------
    a  b  c  d  e  f

e2=
 4 |.  .  .  .  .  .  |
 3 |G  .  .  .  .  .  |
 2 |.  .  .  .  G  .  |
 1 |.  .  .  .  .  .  |
------------------------
    a  b  c  d  e  f

e4=
 4 |.  .  .  .  G  .  |
 3 |.  .  .  .  .  .  |
 2 |.  .  .  .  .  .  |
 1 |.  .  .  .  .  .  |
------------------------
    a  b  c  d  e  f

a1=
 4 |.  .  .  .  .  .  |
 3 |.  .  .  .  .  .  |
 2 |.  .  .  .  .  .  |
 1 |G  .  .  .  .  .  |
------------------------
    a  b  c  d  e  f

a4=
 4 |G  .  .  .  .  .  |
 3 |.  .  .  .  G  .  |
 2 |.  .  .  .  .  .  |
 1 |.  .  .  .  .  .  |
------------------------
    a  b  c  d  e  f
如果我在putChar()函数中对字段[][]=的赋值进行注释,则电路板总是如预期的那样为空,如果我添加其他赋值,有时两个字符都是重复的,有时是一个,有时是任何人

使用putChar()中的固定分配进行测试:

结果总是:

 4 |.  H  .  .  .  .  |
 3 |.  .  .  .  .  H  |
 2 |.  .  .  .  .  .  |
 1 |.  .  .  .  .  .  |
------------------------
    a  b  c  d  e  f
似乎重复数据遵循一种模式,重复总是出现在可能的情况下为-4,1。。。但是为什么呢? 如果阵列限制发生变化,则该阵列随阵列位置-场高(相对于良好阵列1)而变化


有没有办法解决这个问题?

字段[r-1][c]
r中,从
1
FIELDHEIGHT
,但是
字段的第一个维度有
FIELDWIDTH
元素。我想你在这里指的是
字段[c][r-1]

你需要决定哪个维度是X,哪个维度是Y。字段[2][5]='H'。。。似乎是OOB的问题。请先检查pr是否高于FIELDWIDTH-1,pc是否高于FIELDHEIGHT-1。(事实上,我们无法运行您的代码,因此很难找到问题。)谢谢。这是主要问题。我在所有的任务中都检查了这个订单,它按预期工作。请原谅我提出了一个愚蠢的问题,我以前看不到这一点,尽管它很清楚。
void Field::putChar(void){
...
(random definition of pr2 and pc2 and validation within limits)
...
 field[2][5]='H';
...
};
 4 |.  H  .  .  .  .  |
 3 |.  .  .  .  .  H  |
 2 |.  .  .  .  .  .  |
 1 |.  .  .  .  .  .  |
------------------------
    a  b  c  d  e  f