#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2015
    Posts
    5
    Rep Power
    0

    Rock, Paper, Scissors switch statement


    So, i'm new to all this stuff and not used to c++ yet. My problem here is that when i enter 0-2 only case 0 shows and when i enter 1 or 2 it doesn't show .When i put in anything greater than 2, i don't get the default message. I feel like it because of the "breaks;" but i'm not exactly sure. I've tried adding brackets and breaks in different spots. Im using a random number generator for the computers input and switch statements. Also, it outputs two statements that run into each other and the press to continue sometimes shows up next to the statement Help please!


    Code:
    #include <cstdlib>
    #include <iostream>
    #include <complex>
    #include <ctime>
    
    using namespace std;
    
    
    
    int main()
    {
              int choice;
    
        cout << "scissors (0), rock (1), paper (2): ";
        cin >> choice;
        
        
    
            srand(time(0));
            int comp = rand() % 3;
        
        switch (comp){
        
    case 0:
         
    if (choice == 0)
     cout << "The computer is scissors. You are scissors too. It is a draw.";
     break;
    if (choice == 1) 
     cout << "The computer is scissors. You are rock. You win.";
     break;
    if (choice == 2)
      cout << "The computer is scissors. You are paper. You loose.";
    break;
    
    case 1:
         
    if (choice == 0)
     cout << "The computer is rock. You are scissors. You loose.";
     break;
    if (choice == 1) 
     cout << "The computer is rock. You are rock too. It is a draw.";
     break;
    if (choice == 2) 
      cout << "The computer is rock. You are paper. You win.";
    break;
      
    case 2:
         
    if (choice == 0)
     cout << "The computer is paper. You are scissors. You win .";
     break;
    if (choice == 1)
     cout << "The computer is paper. You are rock. You loose.";
     break;
    if (choice == 2)
      cout << "The computer is paper. You are paper. It is a draw.";
      break;
      
    default:
    
     cout << "Invalid number, try again" << endl;
    }
    
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,478
    Rep Power
    1875
    Try it with only ONE break per case.

    As it stands, you always break (despite your poor indentation indicating otherwise) after every if statement.
    None of your breaks are actually part of any if statements - they're all unconditional.

    Comments on this post

    • Will-O-The-Wisp agrees
    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. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2015
    Posts
    5
    Rep Power
    0
    Putting the breaks at the end and the case 1 and 2 are working but i'm still having the issues of the "press to continue...." not moving on to the second line. Also, my default statement still wont. Any ideas?
    Here's what i have now.

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <complex>
    #include <ctime>
    
    using namespace std;
    int main()
    {
        
     int choice;
    
     cout << "scissors (0), rock (1), paper (2): ";
     cin >> choice;
        
        srand(time(0));
        int comp = rand() % 3;
        
        switch (comp){
        
    case 0:
         
    if (choice == 0)
     cout << "The computer is scissors. You are scissors too. It is a draw.";
     
    if (choice == 1) 
     cout << "The computer is scissors. You are rock. You win.";
     
    if (choice == 2)
      cout << "The computer is scissors. You are paper. You loose.";
    break;
    
    case 1:
         
    if (choice == 0)
     cout << "The computer is rock. You are scissors. You loose.";
    
    if (choice == 1) 
     cout << "The computer is rock. You are rock too. It is a draw.";
     
    if (choice == 2) 
      cout << "The computer is rock. You are paper. You win.";
    break;
      
    case 2:
         
    if (choice == 0)
     cout << "The computer is paper. You are scissors. You win .";
     
    if (choice == 1)
     cout << "The computer is paper. You are rock. You loose.";
     
    if (choice == 2)
      cout << "The computer is paper. You are paper. It is a draw.";
      break;
      
    default:
            cout << "Invalid number, try again" << endl;
     break;
     
    }
        system("PAUSE");
        return EXIT_SUCCESS;
    }
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,325
    Rep Power
    2226
    ... but i'm still having the issues of the "press to continue...." not moving on to the second line.
    I believe that endline is endl. Maybe if you were to use it, ... .

    Also, my default statement still wont.
    "wont" what?

    BTW, the switch structure uses comp, which is the computer's choice and which is guaranteed to never go outside the range of 0..2, so the switch will never hit the default case. Maybe you should re-think slightly how you're using that switch?

    Comments on this post

    • Will-O-The-Wisp agrees
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2015
    Posts
    5
    Rep Power
    0
    Yeah i'm stupid for not thinking to put the "endl:" at the end of each statement. Oh and the default still won't show I meant. So your saying to make the switch for the user choice and not the computers choice?
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,782
    Rep Power
    4301
    Originally Posted by Zach4531
    So your saying to make the switch for the user choice and not the computers choice?
    Yep, otherwise you'll never hit the default section, as the computer choice is guaranteed to be between 0 and 2.

    Comments on this post

    • Will-O-The-Wisp agrees
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,325
    Rep Power
    2226
    So your saying to make the switch for the user choice and not the computers choice?
    Bimbo! (that's a computer-geek-friendly movie reference, BTW: "Short Circuit")

    If you want the default to be selected based on the user choice, then the rest of the cases also need to be selected based on the user choice.

    The alternative would be to keep the switch as it is and add an else in each and every case for user choices outside the acceptable range. For one thing, you are using separate if-statements instead of taking advantage of the if-else if-else control structure -- learn aobut that one, because it is very useful. Also, it is considered better practice to do something in just one place instead of doing the same thing in several different places -- for one thing, if you have to change it you will invariably forget one of those several places.
    Last edited by dwise1_aol; March 25th, 2015 at 05:43 PM.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2015
    Posts
    5
    Rep Power
    0
    It Works Now!! Thank you everyone for your help. I was stuck on this forever haha.

IMN logo majestic logo threadwatch logo seochat tools logo