CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 34
  1. #1
    Join Date
    Jan 2013
    Posts
    71

    Question for Rock Paper Scissors game program

    My teacher wants the user to enter in their choices with characters instead of numbers.
    R - Rock
    P - Paper
    S - Scissors.

    After the user enters R for their choice, what would I use to convert the character to a number (int)? Can i use a switch?

    Was trying to use if statements like:

    Code:
    if (guess == R)
                  {
                   userGuess = 1;
                  }
    if (guess == P)
                  {
                   userGuess = 2;
                  }
    if (guess == S)
                  {
                   userGuess = 3; 
                  }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Question for Rock Paper Scissors game program

    My teacher wants the user to enter in their choices with characters instead of numbers.
    Once entered as a char, just use it as a char. For example, something like the following

    Code:
    char choice;
    cout << "Enter choice (R, P, S): ";
    cin >> choice;
    
    switch (choice) {
         case 'R':
         case 'r':
            //code for Rock
           break;
    
         case 'P':
         case 'p':
           //code for Paper
           break;
    
         case 'S':
         case 's':
           //code for Scissors
           break;
    
        default:
          //error handling for incorrect choice
    }
    Last edited by 2kaud; April 21st, 2013 at 04:39 PM. Reason: Fixed quote problem
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jan 2013
    Posts
    71

    Re: Question for Rock Paper Scissors game program

    well the computer generating it's choices as 1, 2 or 3. So how do i compare

  4. #4
    Join Date
    Jan 2013
    Posts
    71

    Re: Question for Rock Paper Scissors game program

    ok, i think i can compare. so ill work on that. ty

  5. #5
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Question for Rock Paper Scissors game program

    try this

    Code:
    enum SELECTION {
        ROCK = 1,
        SCISSORS = 2,
        PAPER = 3,
        LIZARD = 4,
        SPOCK = 5
    };

  6. #6
    Join Date
    Jan 2013
    Posts
    71

    Re: Question for Rock Paper Scissors game program

    Love Big Bang Theory!

    But, teacher wants user to imput letters. Im not doing very well trying to compare an int to a char.

    Any ideas? Ive tried some things that compile, but they dont run.

    EDIT. Let me also explain that the computer i do have it randomly selecting 1,2 or 3 for rock, paper, scissors. But the user input has to be R, P or S for rock paper scissors.

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Question for Rock Paper Scissors game program

    Quote Originally Posted by psfign View Post
    Love Big Bang Theory!

    But, teacher wants user to imput letters. Im not doing very well trying to compare an int to a char.

    Any ideas? Ive tried some things that compile, but they dont run.

    EDIT. Let me also explain that the computer i do have it randomly selecting 1,2 or 3 for rock, paper, scissors. But the user input has to be R, P or S for rock paper scissors.
    Don't compare a number to a letter. Convert a letter to a number and compare two numbers, or you could follow the example that 2kaud gave using the switch statement.

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Question for Rock Paper Scissors game program

    I gave you an example in post #2 how to handle user input of R, P or S. Use a similar method for the 1, 2, or 3 for the computer go. If you post your code so far as we'll be able to give better guidance.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Jan 2013
    Posts
    71

    Re: Question for Rock Paper Scissors game program

    yea, i havent looked at doing that switch 2k gave yet. But a couple follow up questions. Am i correct in the thought that i could do something like:

    Code:
    num = Computer's selection
    
    if (num == 1)
    return 'R';
    or on the switch like 2k gave...can i do for the user input:

    Code:
    switch (choice) {
         case 'R':
         case 'r':
            return 1;
           break;
    either one of those would be ok?

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Question for Rock Paper Scissors game program

    Those could work if you use them properly.

  11. #11
    Join Date
    Jan 2013
    Posts
    71

    Re: Question for Rock Paper Scissors game program

    Yea I know 2k, but i was trying other things that i thought would work....that obviously didnt work.

    and looking at your example, im assuming i can use return '1' as code for Rock

  12. #12
    Join Date
    Jan 2013
    Posts
    71

    Re: Question for Rock Paper Scissors game program

    Quote Originally Posted by GCDEF View Post
    Those could work if you use them properly.
    Thanks!

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Question for Rock Paper Scissors game program

    Why don't you convert 1 or 2 or 3 for the computers guess to the char 'R', or 'P' or 'S' - then use the char everywhere?

    Code:
    const char conv[] = "RPSB";
    ....
    char guess = (num >= 1 && num <= 3) ? conv[num - 1] : conv[3];
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    Join Date
    Jan 2013
    Posts
    71

    Re: Question for Rock Paper Scissors game program

    We havent learned how to do the conv like that, so i think I'd have to do it as

    Code:
    num = Computer's selection
    
    if (num == 1)
    return 'R';
    Thanks for your help again guys! I didnt post my code yet bc i dont want you guys thinking im trying to get you to do it for me.

    I'm wondering if I should throw in the Lizard & Spock options. Not sure how the prof would react!

  15. #15
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Question for Rock Paper Scissors game program

    i'm wondering if i should throw in the lizard & spock options. Not sure how the prof would react!
    a++
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured