-
September 21st, 2015, 11:36 AM
#1
adding statement
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.
Code:
#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
-
September 21st, 2015, 12:44 PM
#2
Re: adding statement
This has been addressed elsewhere.
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
|