SOLVED: Help fixing problem in C++ project

So for school I am working on a mancala project. I’ve been trouble shooting and I think I’ve finally narrowed down where the problem is, which is within the if statement:
if (sum1 == 0 ||sum2==0)

After this all ends, I return the winner. The winner is returning, however, it keeps returning -1 even if both sums are equal (I have them outputted to keep track of what they are). The variable winner should be 3 instead of -1.

I’m hoping this is something silly and obvious I’ve just been overlooking.

I’ve only posted the function where I’m pretty certain the problem is, but if you want to see the whole project I can post that as well.

I put outputs inside the if statements that change the winner, and none of them are outputting, so this means that my program isnt even entering the if statement in question and is just outputting the -1. sum1 and sum2 both equal zero right now (I have a function that turns all the array values into zeros for testing), so it should be entering it but it isnt.

I didn’t want to post this in the project section because this project isn’t complete. Any help is greatly appreciated!

int gameOverCheck(int beadArray[MAX])
{

int winner = -1; //returns - 1 when game is not over/ returns 1 or 2 to return who won
int sum1 = 0; //holds player 1s bin values
int sum2 = 0; //holds player 2s bin values

for (int i = 0; i <6; i++) //counts beads on player1's side of board
{
	cout <<endl << "sum1" << endl;
	sum1 = sum1 + beadArray[i];
	cout << endl << sum1 << endl;
}

for (int i = 0; i < MAX; i++) //counts beads on player2's side of board
{
	if (i >= 6) // "filter" to make sure that only the second half is being added to sum2
	{
		cout << endl << "sum2" << endl;
		sum2 = sum2 + beadArray[i];
		cout << endl << sum2 << endl;
	}
}

if (sum1 == 0 || sum2 == 0) // checks if either player has all their main bins empty
{
	/* Note: beadArray[13] is bin 6 and beadArray[6] is bin 13.*/
	
	if (beadArray[6] < beadArray[13]) //if player 1 has more beads than player 2
	{
		winner = winner + 1;
	
	}
	if (beadArray[13] < beadArray[6]) //if player 2 has more beads than player 1.
	{
		winner = winner + 2;
		
	}
	if (beadArray[13] = beadArray[6])
	{
		winner = 3; //in case of tie
		
	}
	

	
	
	
	
} //end of gameover
return winner;
//end of fuction

}

You’re going to kick yourself but in this line

if (beadArray[13] = beadArray[6])

you only have = which assigns instead of == which tests for equality. With this change your code works properly.

1 Like

YOU’RE RIGHT!! IT WORKED!! THANK YOU SO MUCH!

edit- i deleted the first reply because I was sourcing from the wrong code lol. Either way you were right and I really appreciate your help.

No problem. To avoid these kinds of problems in the future, enable all warnings from your compiler. (e.g. -Wall in gcc/g++ on Linux.) Modern compilers are smart enough to flag constructs like this as potential mistakes.

1 Like

Oh ok! Ill try that out. Thanks a bunch