Thread: else statement acting up

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    7

    else statement acting up

    hey again guys. Another quick question. I cant seem to figure out why this is printing out my else statement under the do while statement in main when 1 is selected. If 2 is selected it does not print. Any pointers?!

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int rollDiceOne(int scoreOne)
    {
        int roll=0;
        roll=rand()%6+1;
        printf("You rolled a %d\n", roll);
        if (roll==1)
        {
            scoreOne=0;        
            printf("You lose all your points! Better luck next roll.\n");
        }
        else
        {
            scoreOne=roll+scoreOne;
        }
    return scoreOne;
    }
    
    int rollDiceTwo(int scoreTwo)
    {
        int roll=0;
        roll=rand()%6+1;
        printf("You rolled a %d\n", roll);
        if (roll==1)
        {
            scoreTwo=0;        
            printf("You lose all your points! Better luck next roll.\n");
        }
        else
        {
            scoreTwo=roll+scoreTwo;
        }
    return scoreTwo;
    }
    
    
    
    
    int main (void)
    {
        int answer=0;    
        int roll=0;
        int scoreOne=0;
        int scoreTwo=0;
        int choice=0;
    
    
            do
            {
    
            printf("\n\n\nWhat would you like to do?\n");
            printf("Lets play the Game Of Pig!!..........1\n");
            printf("What are the rules?..................2\n");
            printf("Exit the programs....................3\n\n");
            scanf("%d", &answer);
    
            
            switch(answer)
                {
                case 1:                
                    printf("Lets get started!");
                        do
                        {
                            
                            printf("\n\n\n_________Player 1_________\nWhat would you like to do?\n");
                            printf("Select 1 to skip turn and add one point.\nSelcet 2 to roll dice.\n");
                            scanf("%d", &choice);                        
                                if (choice==1)    
                                {
                                    scoreOne=scoreOne+1;
                                    printf("Player one's score is %d\n", scoreOne);            
                                }    
                                if (choice==2)
                                {
                                    
                                    scoreOne=rollDiceOne(scoreOne);        
                                    
                                    printf("Player one's score is %d\n", scoreOne);
                                }
                                else
                                {
                                    printf("That's not a valid option. Please select 1 or 2.\n");
                                }
                            if (scoreOne>50)
                            {
                                scoreOne=scoreOne-15;
                                printf("Sorry! You went over 50! You lose 15 points!\n");    
                            }
                            if (scoreOne==50)
                            {
                                printf("Congratulations!!!! PLAYER 1 WINS!!!\n");
                                exit(0);
                            }
                            
                            printf("\n\n\n_________Player 2_________\nWhat would you like to do?\n");
                            printf("Select 1 to skip turn and add one point.\nSelcet 2 to roll dice.\n");
                            scanf("%d", &choice);
                                if (choice==1)    
                                {
                                    scoreTwo=scoreTwo+1;
                                    printf("Player two's score is %d\n", scoreTwo);            
                                }    
                                if (choice==2)
                                {
                                                                
                                    scoreTwo=rollDiceTwo(scoreTwo);
                                    
                                    printf("Player two's score is %d\n", scoreTwo);
                                }
                                else
                                {
                                    printf("That's not a valid option. Please select 1 or 2.");
                                }
                            if (scoreTwo>50)
                            {
                                scoreTwo=scoreTwo-15;
                                printf("Sorry! You went over 50! You lose 15 points!\n");    
                            }
                            if (scoreOne==50)
                            {
                                printf("Congratulations!!!! PLAYER 2 WINS!!!\n");
                                exit(0);
                            }
                        }while(scoreOne!=50, scoreTwo!=50);
                    
                    
                    break;        
                case 2:
                    printf("\n\n\nThe rules are simple. The goal of the game of pig is to get a score of 50!\n");
                    printf("Each player will have 2 choices upon their turn.\n");
                    printf("Choice 1: Skip their turn and automatically gain 1 point\n");
                    printf("Choice 2: Roll the dice. If you roll a 1 then all of your points are forfit.\n");
                    printf("\tIf you roll anything else, what ever you roll, that number will be added\n\tto your score\n");
                    printf("If your score goes over 50, the you lose 15 points and must attempt to get 50 again!\n");
                    break;
                case 3:
                    exit(0);
                    break;
                default:
                    answer=-1;
                    printf("Thats not a valid option! Try again!\n");                
                    break;
                }    
            }
            while(answer=-1);
    return 0;
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Could you please give a sample of what you are inputing and what the output is?
    (Do not forget to say again why you are unhappy with this )

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    7
    well the program adds the point like its supposed to but then it prints off the else statement as well. It shouldnt be printing off the else statement unless i enter something other than 1 or 2. but never the less it prints it off anyway. Sorry, i dont know how else to explain it. sorry, i dont know how else to explain it. programming hurts my brain :-(

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I guess you are talking about this
    Code:
     scanf("%d", &choice);                        
                                if (choice==1)    
                                {
                                    scoreOne=scoreOne+1;
                                    printf("Player one's score is %d\n", scoreOne);            
                                }    
                                if (choice==2)
                                {
                                     
                                    scoreOne=rollDiceOne(scoreOne);        
                                     
                                    printf("Player one's score is %d\n", scoreOne);
                                }
                                else
                                {
                                    printf("That's not a valid option. Please select 1 or 2.\n");
                                }
                                ....
    Well , let's say I input 1. Then it will print the message of the first if-body. Ok until now.Then it will go to the if choice is equal to 2. It will find out that the condition is false, thus it will step in the else body.

    What you need is something like this
    Code:
    if(choice==1)
    {
            ...
    }
    else if(choice==2)
    {
           ...
    }
    else
    {
          /* I will come here only if choice */
          /* is not 1 AND it is not 2      */
    }

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    7
    Ahhh got cha. knew it would be something silly like that! thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While is acting strangely
    By JayCee++ in forum C++ Programming
    Replies: 2
    Last Post: 06-08-2012, 09:06 AM
  2. Is my compiler acting up?
    By Alastor in forum C Programming
    Replies: 10
    Last Post: 10-11-2005, 07:19 PM
  3. mmsystem.h acting up
    By Frantic- in forum Windows Programming
    Replies: 2
    Last Post: 06-17-2005, 08:38 PM
  4. vectors acting up?
    By hsv in forum C++ Programming
    Replies: 1
    Last Post: 06-04-2003, 04:45 AM
  5. getch() acting up...
    By Govtcheez in forum C Programming
    Replies: 9
    Last Post: 08-18-2001, 12:56 PM