# Thread: Need help figuring out why my short program wont run correctly

1. ## Need help figuring out why my short program wont run correctly

Hello all,
I am basically trying to create a game where there are 32 marbles in a bag, there are 2 players, the objective is for the bag to be left with 1 marble. Each player has a choice to take out 1,2, or 3 marbles on each turn.

I have my program running okay, subtracting values from 32 until it reaches 1 however it does not stop at 1 it keeps going to down to negative numbers. I dont understand why this is because I have if statements in there. Can someone take a look?
This should be very simple...

Code:
```#include <stdio.h>

int main() {

int num_marbles1, num_marbles2, all_marbles=32;

printf("There are 32 marbles remaining.\n\n");

while(all_marbles!=1){
printf("How many marbles would you like to take? ...Player 1\n");
scanf("%d", &num_marbles1);
printf("There are %d marbles remaining\n\n", all_marbles=all_marbles-num_marbles1);
printf("How many marbles would you like to take? ...Player 2\n");
scanf("%d", &num_marbles2);
printf("There are %d marbles remaining.\n", all_marbles=all_marbles-num_marbles2);

if (all_marbles-num_marbles1==1) {
printf("\nPlayer 1 Wins.\n");
}

else if (all_marbles-num_marbles2==1) {
printf("\nPlayer 2 Wins.\n");

}
}

system ("PAUSE");
return 0;

}```

2. Here's your logic (in a nutshell)

1) get # of marbles to reduce for player1
2) reduce total # of marbles by player1 value for new adjusted total
3) get # of marbles to reduce for player2
4) reduce adjusted total # of marbles by player2 value for new adjusted total marbles
5) if new adjusted total marbles minus player1 entered value == 1, declare him winner
6) else if new adjusted total marbles minus player2 entered value == 1, declare him winner

You logic should be

1) get # of marbles to reduce for player1
2) if value entered is < 1 or > 3, error, get new player1 value
3) if subtracting # of player1 marbles is from all_marbles is < 1, error, get new player1 value
4) reduce total # of marbles left
5) if # of total marbles remaining == 1, playerA wins

steps 6-10 are the same, but for player2.

3. If you read this SourceForge.net: Indentation - cpwiki

4. Thanks for the reply Salem. Your right I need to indent! Your right, I apologize, I'm a beginner...

5. Wow. Maybe if I would have slapped your hand instead of answering your question I would have gotten a "thank you" too.

Here, I'll try that...

It's not