Thread: Assignment: Rock, Paper, Scissors

  1. #1
    Registered User
    Join Date
    Aug 2020
    Posts
    1

    Assignment: Rock, Paper, Scissors

    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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    1. You don't initialise your tie variable.
    2. Your function lacks a return statement.
    3. In main, you don't call the function (but you do create a variable with the same name as your function).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Rock Paper Scissors
    By Jihan in forum C Programming
    Replies: 6
    Last Post: 11-05-2014, 07:17 AM
  2. Rock, Paper, Scissors
    By Slynet in forum C Programming
    Replies: 2
    Last Post: 02-13-2009, 07:14 PM
  3. rock paper Scissors
    By jackstify in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2007, 10:16 PM
  4. Paper Scissors Rock v0.1
    By Kirdra in forum Game Programming
    Replies: 2
    Last Post: 09-14-2002, 11:32 AM
  5. Rock, Paper, Scissors
    By Twiggy in forum C Programming
    Replies: 9
    Last Post: 11-06-2001, 05:06 AM

Tags for this Thread