C 2D char阵列不当比较
C++ 2d Char array improperly comparing
im创建一个连接四,试图实现Drop功能,添加到特定列中的最低行。这是董事会的初始化
Board::Board()
{
for(int i=0;i<8;i++)
for(int j=0;j<8;j++)
place[i][j] = EMP; // EMP is a const char = '-'
cout << "Initalized.n";
}
由于某种原因,此代码一直运行到i = 1,然后将位置[1] [col]设置为 *但是当我去显示此内容时,它在数组的底部显示 * *
也是这个 ->一开始就在cout中的位置和放置,永远不要给我输出。
。int Board::add(int player, int col)
{
char piece;
col--; // Dealing with array starting at 0, not 1
(player==1) ? piece = P1: piece = P2; // Character defining players piece
int i;
for (i = 7; i >= 0; i--)
{
cout << "this - " << this->place[i][col] << endl;
cout << "place - " << place[i][col] << endl;
if(place[i][col] == EMP)
{
cout << "Empty looks like " << place[i][col] << "ti: " << i << endl;
place[i][col] = piece;
system("pause");
return i;
}else
{
cout << "not EMP - " << place[i][col] << endl;
system("pause");
}
}
return 0;
}
您将char传递到int函数中,该功能将数字转换为char变量(给您垃圾)。我重写了一些功能,一切似乎都很好。
int Board::add(int player, char col)
{
char piece;
int Num;
Num = atoi(&col);
cout << Num << endl << endl;
Num--; // Dealing with array starting at 0, not 1
(player==1) ? piece = P1: piece = P2; // Character defining players piece
int i;
for (i = 7; i >= 0; i--)
{
cout << "this - " << this->place[i][Num] << endl;
cout << "place - " << place[i][Num] << endl;
if(place[i][Num] == EMP)
{
cout << "Empty looks like " << place[i][Num] << "ti: " << i << endl;
place[i][Num] = piece;
return i;
}else
{
cout << "not EMP - " << place[i][Num] << endl;
}
system("pause");
}
return 0;
}
希望这会有所帮助。
一些想法:
不要delete place
,您从未使用new
获得内存。
这样使用有条件的操作员是更惯用的: piece = (player==1) ? P1: P2;
初始化,添加和显示的代码在这里几乎可以使用:http://codepad.org/qiwhagmk
您是否可以更具体地说出您的要求或显示一个较小的代码片段,以证明您看到的问题?
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么比较运算符如此快速
- 比较两个std::矢量/阵列,或者通常比较两个stl连续器
- 简单的程序比较阵列运行但给出不正确的结果
- 将动态阵列中的字符与char进行比较
- 比较阵列的更好方法
- TBB免费图像lambda阵列比较错误
- 如何在C/ C 中比较两个char的两个2D阵列
- 比较从文本文件中读取的并行阵列
- 2D阵列和1D阵列存储比较
- 通过使用指针(C )比较阵列中的元素
- 比较阵列相似性的最佳比较算法是什么?
- 整数的阵列比较
- 如何比较两个QString阵列Qt C++
- 比较C 中字符阵列表中的项目
- C 2D char阵列不当比较
- 我是否必须求助于循环,以比较C 中的两个2D阵列
- 一个二维动态阵列的C++剖分与比较
- 多维阵列与平面阵列 - 性能比较
- 将检测到的人脸与现有人脸阵列与 android 中的 opencv 进行比较