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

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    244

    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. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    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.
    Last edited by Dino; 03-20-2010 at 04:14 PM.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    If you read this SourceForge.net: Indentation - cpwiki
    more people would read your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Thanks for the reply Salem. Your right I need to indent! Your right, I apologize, I'm a beginner...

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    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
    ... Your right I need to indent! Your right, ...
    it's

    ... You're right I need to indent! You're right, ...
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. triggering another program to run using serial port
    By infineonintern in forum C++ Programming
    Replies: 3
    Last Post: 07-22-2009, 05:16 AM
  2. Help on making program run more efficiently
    By peeweearies in forum C Programming
    Replies: 2
    Last Post: 03-23-2009, 02:01 AM
  3. Re-doing a C program to run in Win2000 or XP
    By fifi in forum C Programming
    Replies: 5
    Last Post: 08-17-2007, 05:32 PM
  4. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  5. Replies: 5
    Last Post: 04-17-2003, 07:07 AM