Title: Trying to count specific char in 2D array, but entire array is counted instead
Tags: c++;arrays;multidimensional-array;char;cout
Question: I am trying to count how many times a specific character appears in a 2D array. The problem is that it is counting all characters in the array instead. I'm not sure why.
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
Comment: lol! I can't believe I missed that! It's been a long night. That's all I had to fix. I guess this was just one of those things where I spent too long looking for the wrong issues and overlooked the simple stuff. I'm using Visual Studio Pro for this, which has been bugging lately and requiring restarts. Sometimes, for example, it gives errors where there are no problems requiring a restart. Been making things more difficult.

Comment: You *do* know the difference between comparison for equality and assignment? Maybe you need to turn on more warnings for the compiler, because it would detect the problem and warn you about it.

Here is the accepted answer: It's just a typo..
if (temp = 'P')
You are using assignment operator instead of comparison.
Comment for this answer: yeah, thanks. lol. That fixed it. I still can't believe I overlooked something so simple. Well, hopefully this question can still be useful to someone else since it's both an example of counting all characters and counting one character.

Here is another answer: To test for equality use == and not = (which stands for assignment).
The line "if (temp = 'P')" means that you are assigning 'P' to temp and then testing if temp is different from 0 (the null character), which of course it is ('P' is different from 0!).
The correct way to write the test is:
if (temp == 'P')
Also, why is the limit in the first for loop different from that in the second for loop? Are you sure about this?
Finally, why do you use x and y to name variables of integer type, wouldn't it be better if you used i and j instead?
Comment for this answer: The reason is for different values is because I am storing the new line between boards and using that as a measure for checking how many boards to calculate, but it is still an 8x8 board so I am only going 8 spaces wide.
I have already set it up to check for and remove white spaces between board positions so there's no problem there, and yeah, after the typo was corrected everything works.

Here is another answer: Change if (temp = 'P') to if (temp == 'P')

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:01/29/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c++><arrays><multidimensional-array><char><cout>', 'Title': 'Trying to count specific char in 2D array, but entire array is counted instead', 'ViewCount': 346, 'id': 35077469, 'user_AboutMe': '<p>When I ask questions I try to keep it to the point. I use the minimum required code to get the point across. Unfortunately, that isn\'t good enough for most people. I am fed up with having to bend over backwards for every stupid little thing. </p>\n\n<p>If I say "Here are two lines of code that work. How can I do this better?" I don\'t want to have to paste an entire program because a troll insists that there is a memory hole in the non existent program and I am NOT going to do that anymore. </p>\n\n<p>If I say "Here\'s the area of code where I am having a problem" I am tired of so many people saying "It must be somewhere else" or "Show me a complete program" and I am even more so tired of questions being flagged as off topic whenever I actually paste a complete program. If you don\'t know the answer to my question, please, PLEASE don\'t act like a know-it-all. Accept that you don\'t know and move on! I\'m not going to waste my time satisfying people like that anymore so just stop!</p>\n\n<p>and if the down votes from such people get me banned? Fine, stackexchange is clearly the kind of website where you either earn up votes by trolling or delete then recreate an account with a new IP address every so often. It sucks that stackexchange is designed to reward idiots and trolls, but whatever.</p>\n', 'user_AccountId': 4721005.0, 'user_DisplayName': 'Elliander', 'user_Reputation': 493.0, 'user_Views': 80.0, 'user_id': 4587040.0}}