Rock Paper Scissors

how can i make another statement when i enter other letters excluding R,P,S printf will be Invalid Letter. I hope someone can help im just beginning in turbo c++

the code is not mine. The code is working properly i just want to add a new statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  #include <stdlib.h> 
#include <ctype.h> 
/* 
DEFINE FUNCTION GetInput( Input Text as string ) returns character 
..declare result as character value ' ' 
..LOOP WHILE ( result is no R S or P ) 
....output (text) (Rock, Scissors, Paper) 
....input result 
..END LOOP 
..return result 
END FUNCTION 
*/ 
char GetInput(const char *szPlayer) 
{ 
char cRet = 0; 
while(!(cRet == 'S' || cRet == 'R' || cRet == 'P')){ 
printf("%s: (R)ock, (S)cissors, (P)aper > ", szPlayer); 
fflush(stdin); 
scanf("%c", &cRet); 
cRet = toupper(cRet); 
} 
return cRet; 
} 
int main() 
{ 
const char *msg[] = { 
"Paper covers Rock", 
"Scissor cuts paper", 
"Rock breaks Scissors", 
"It's a Draw!" 
}; 

const char *win[] = { 
", Player 1 wins!\n", 
", Player 2 wins!\n" 
}; 
/* ..declare Player1 as character value ' ' 
..declare Player2 as character value ' '*/ 
char cPlayer1 = 0, 
cPlayer2 = 0; 
/* 
MAIN ENTRY POINT 
..Player1 = GetInput("Player 1") 
..CLEAR SCREEN 
..Player2 = GetInput("Player 2") 
..CLEAR SCREEN 
END MAIN 
*/ 

cPlayer1 = GetInput("Player 1"); 
system("cls"); 
cPlayer2 = GetInput("Player 2"); 
system("cls"); 
/* 
..IF( ( Player1 is Paper ) AND (Player2 is Rock) ) THEN output Paper covers Rock, Player 1 wins! 
..ELSE IF( ( Player1 is Rock) AND (Player2 is Scissors) ) THEN output Rock breaks scissors, Player 1 wins! 
..ELSE IF( ( Player1 is Scissors) AND (Player2 is Paper) ) THEN output Scissors cuts paper, Player 1 wins! 
..ELSE IF( ( Player2 is Paper ) AND (Player1 is Rock) ) THEN output Paper covers Rock, Player 2 wins! 
..ELSE IF( ( Player2 is Rock) AND (Player1 is Scissors) ) THEN output Rock breaks scissors, Player 2 wins! 
..ELSE IF( ( Player2 is Scissors) AND (Player1 is Paper) ) THEN output Scissors cuts paper, Player 2 wins! 
..ELSE output Its a Draw! 

*/ 
if (cPlayer1 == 'P' && cPlayer2 == 'R') printf("%s%s", msg[0], win[0]); 
else if (cPlayer1 == 'R' && cPlayer2 == 'S') printf("%s%s", msg[2], win[0]); 
else if (cPlayer1 == 'S' && cPlayer2 == 'P') printf("%s%s", msg[1], win[0]); 
else if (cPlayer2 == 'P' && cPlayer1 == 'R') printf("%s%s", msg[0], win[1]); 
else if (cPlayer2 == 'R' && cPlayer1 == 'S') printf("%s%s", msg[2], win[1]); 
else if (cPlayer2 == 'S' && cPlayer1 == 'P') printf("%s%s", msg[1], win[1]); 
else printf("%s\n", msg[3]); 
system("pause"); 
return 0; 
}


Need a help :)
Maybe try if( cRet != 'S' || cRet != 'R' || cRet != 'P' )
@YFGHNG

That statement will always return true which is not what you want.
The OP's code at line 16 is correct. Note the ! at the beginning of the expression.

@OP
i just want to add a new statement.

Not clear what you want to add.
@AbstractionAnon

According to the OP..

how can i make another statement when i enter other letters excluding R,P,S printf will be Invalid Letter.


I think the OP wants to print out something telling the user that the inputted letter is not recognized as a legit input.
@whitenite1

yes.

when i enter other letters the printf must be "Invalid Letter"
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,q,t,u,w,x,y,z these letters must be the one Invalid Letter when i try to enter this in the game.

i hope someone can help me :D
Try to add another if inside while with same condition as while
@LendeaDwi
Just like this ? sorry dont have any idea in this , im just a beginner

while(!(cRet == 'S' || cRet == 'R' || cRet == 'P'))
{
"if statement"
printf("%s: (R)ock, (S)cissors, (P)aper > ", szPlayer);
fflush(stdin);
scanf("%c", &cRet);
cRet = toupper(cRet);
}
@anonymous018

Here's a new GetInput() function, with the added input check you're looking for. Not sure if C needed it, but C++ did. I had to add #include <string> to the program. If you don't need it, great.

1
2
3
4
5
6
7
8
9
10
11
12
13
char GetInput(const char *szPlayer) 
{ 
char cRet = ' '; 
while(!(cRet == 'S' || cRet == 'R' || cRet == 'P')){ 
printf("%s's choices are : (R)ock, (S)cissors, (P)aper > ", szPlayer); 
fflush(stdin); 
scanf("%c", &cRet); 
cRet = toupper(cRet); 
if(cRet !='P' && cRet !='R' && cRet != 'S') // Added these 2 lines
  printf("Sorry. That was not one of the choices. Try again, please.\n\n");
} 
return cRet; 
} 
@whitenite1 i will try this later :) thanks for helping me guys :)
@whitenite1 it worked .. thanks :)
I find that the cleanest way to do this sort of checking is
1
2
3
4
5
6
7
8
while (true) {
    get the input
    if (input is valid) {
        break;
    } else {
        print error message
    }
}
Topic archived. No new replies allowed.