【发布时间】:2016-05-06 18:31:40
【问题描述】:
我正在尝试计算特定字符在二维数组中出现的次数。问题是它正在计算数组中的所有字符。我不知道为什么。
int P = 0;
int p = 0;
int R = 0;
int r = 0;
int N = 0;
int n = 0;
int B = 0;
int b = 0;
int Q = 0;
int q = 0;
for (int x = 0; x < BOARD_SIZE + 1; x++) // This side is 9
{
for (int y = 0; y < BOARD_SIZE; y++) // This side is 8
{
char temp = ' '; // Tried using temp as pass through, but no difference
temp = chessBoards[x][y];
// cout << temp; // When this is on it prints each element of the array list in order with a number beside it
if (temp = 'P') // This is just one character I am trying to count
{
++P; // It increments for every character so it's not checked properly
}
}
}
cout << P << endl << endl; // This is what I am using to test output
【问题讨论】:
-
您确实知道相等比较和赋值比较之间的区别吗?也许您需要为编译器打开更多警告,因为它会检测到问题并警告您。
-
哈哈!我不敢相信我错过了!这是一个漫长的夜晚。这就是我必须解决的所有问题。我想这只是我花了太长时间寻找错误问题而忽略了简单问题的事情之一。我正在为此使用 Visual Studio Pro,它最近一直在窃听并需要重新启动。例如,有时它会在没有问题需要重新启动的情况下给出错误。让事情变得更加困难。
标签: c++ arrays multidimensional-array char cout