-
April 21st, 2013, 02:46 PM
#1
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;
}
-
April 21st, 2013, 03:08 PM
#2
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)
-
April 21st, 2013, 03:11 PM
#3
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
-
April 21st, 2013, 03:32 PM
#4
Re: Question for Rock Paper Scissors game program
ok, i think i can compare. so ill work on that. ty
-
April 22nd, 2013, 10:29 AM
#5
Re: Question for Rock Paper Scissors game program
try this
Code:
enum SELECTION {
ROCK = 1,
SCISSORS = 2,
PAPER = 3,
LIZARD = 4,
SPOCK = 5
};
-
April 22nd, 2013, 01:47 PM
#6
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.
-
April 22nd, 2013, 02:23 PM
#7
Re: Question for Rock Paper Scissors game program
Originally Posted by psfign
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.
-
April 22nd, 2013, 02:25 PM
#8
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)
-
April 22nd, 2013, 02:33 PM
#9
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?
-
April 22nd, 2013, 02:43 PM
#10
Re: Question for Rock Paper Scissors game program
Those could work if you use them properly.
-
April 22nd, 2013, 02:47 PM
#11
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
-
April 22nd, 2013, 02:50 PM
#12
Re: Question for Rock Paper Scissors game program
Originally Posted by GCDEF
Those could work if you use them properly.
Thanks!
-
April 22nd, 2013, 03:01 PM
#13
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)
-
April 22nd, 2013, 03:10 PM
#14
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!
-
April 22nd, 2013, 03:14 PM
#15
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)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|