Hello! I have been trying to fix my program but it does not work because it only outputs TIE May I ask what I am missing or what I need to fix? Thank you
Rock=1 Paper=2 Scissors=3
The program assumes that the user will always input a valid input (1, 2, or 3). (There is no need to check for invalid input.) The program then outputs one of the following messages: “PLAYER 1 WINS!”, “PLAYER 2 WINS!”, or “TIE.”
Code:
#include <stdio.h>
int findWinner (int nP1, int nP2, int nWinner)
{
int tie;
if (nP1==1 && nP2==2) {
nWinner=nP2;
}
else if (nP1==1 && nP2==3) {
nWinner=nP1;
}
else if (nP1==2 && nP2==1) {
nWinner=nP1;
}
else if (nP1==2 && nP2==3) {
nWinner=nP2;
}
else if (nP1==3 && nP2==1) {
nWinner=nP2;
}
else if (nP1==3 && nP2==2) {
nWinner=nP1;
}
else if (nP1==1 && nP2==1) {
nWinner=tie;
}
else if (nP1==2 && nP2==2) {
nWinner=tie;
}
else if (nP1==3 && nP2==3) {
nWinner=tie;
}
}
int main()
{
int nP1, nP2;
int nWinner;
printf ("Player 1: ");
scanf ("%d",&nP1);
printf ("Player 2: ");
scanf ("%d",&nP2);
int findWinner;
if (nWinner==nP1) {
printf ("PLAYER 1 WINS!\n");
}
else if (nWinner==nP2) {
printf ("PLAYER 2 WINS!\n");
}
else {
printf ("TIE.\n");
}
return 0;
}
Sample Run should be this:
Player 1: 1
Player 2: 2
PLAYER 2 WINS!Player 1: 3
Player 2: 3
TIE.