Jenn Taylor
Jenn Taylor

Reputation: 9

Trouble with Rock-Paper-Scissors

I've been working on a project for school, making a rock-paper-scissors game using functions. This is the code I have so far, but the program is having some problems. This is what the program is returning right now.

Rock, Paper, Scissors

~~~~~~~~~~~~~~~~~~~~~

Rock: 1

Paper: 2

Scissors: 3

2

You picked:

Computer picked:



Press any key to continue . . .

It is not displaying any of the strings, and I don't know enough about the language to figure out why :( I would appreciate some help/pointers/fixes on the code. I'm not using std namespace because a guide I found suggested that it might be causing the issue.

#include <cmath>
#include <iostream>
#include <string>

std::string GetComputerChoice(std::string ComputerChoice)
{
   double Computer;
   Computer = rand() % 3 + 1;

   if (Computer == 1)
   {
      ComputerChoice == "rock";
      return ComputerChoice;
   }
   else if (Computer == 2)
   {
      ComputerChoice == "paper";
      return ComputerChoice;
   }
   else
   {
      ComputerChoice == "scissors";
      return ComputerChoice;
   }
}

std::string GetUserChoice(std::string UserChoice)
{
   double User;
   std::cin >> User;

   if (User < 1 || User > 3)
   {
      std::cout << "Invalid entry ";
      system("pause");
      return 0;
   }
   else if (User = 1)
   {
      UserChoice == "rock";
      return UserChoice;
   }
   else if (User = 2)
   {
      UserChoice == "paper";
      return UserChoice;
   }
   else
   {
      UserChoice == "scissors";
      return UserChoice;
   }
}

std::string DetermineWinner( std::string UserChoice, std::string ComputerChoice, std::string Victory )
{
   Victory == "Something Has Gone Wrong";

   if (ComputerChoice == UserChoice)
   {
      Victory == "Tie!";
      return Victory;
   }
   else if (UserChoice == "rock")
   {
      if (ComputerChoice == "paper")
      {
         Victory == "Paper beats rock! You lose!";
         return Victory;
      }
      else
      {
         Victory == "Rock smashes scissors! You win!";
         return Victory;
      }
   }
   else if (UserChoice == "paper")
   {
      if (ComputerChoice == "rock")
      {
         Victory == "Paper beats rock! You win!";
         return Victory;
      }
      else
      {
         Victory == "Scissors cut paper! You lose!";
         return Victory;
      }
   }
   else
   {
      if (ComputerChoice == "paper")
      {
         Victory == "Scissors cut paper! You win!";
         return Victory;
      }
      else
      {
         Victory == "Rock smashes scissors! You lose!";
         return Victory;
      }
   }
}

int main()
{
   std::cout << "Rock, Paper, Scissors" << std::endl;
   std::cout << "~~~~~~~~~~~~~~~~~~~~~" << std::endl;
   std::cout << "Rock: 1" << std::endl;
   std::cout << "Paper: 2" << std::endl;
   std::cout << "Scissors: 3" << std::endl;

   std::string UserChoice, ComputerChoice, Victory;

   GetUserChoice( UserChoice );
   GetComputerChoice( ComputerChoice );
   DetermineWinner( UserChoice, ComputerChoice, Victory);

   std::cout << "You picked: " << UserChoice << std::endl;
   std::cout << "Computer picked: " << ComputerChoice << std::endl;
   std::cout << Victory << std::endl;
   system("pause");
   return 0;
}

Upvotes: 0

Views: 263

Answers (2)

Carcigenicate
Carcigenicate

Reputation: 45760

You can't reassign the arguments to the function like that. You need to use the return value of GetUserChoice instead. Get rid of the parameter to the Get functions, since it isn't doing anything, and reassign instead assign the return value from the function to the variable:

UserChoice = GetUserChoice();

As a side note, please look up proper naming conversations. Arbitrary variable names shouldn't be capitalized like that. Capitalized identifiers are reserved primarily for class names.

Upvotes: 5

frslm
frslm

Reputation: 2978

You've got a couple issues:

  1. You're incorrectly comparing strings when you should be assigning them:

    ComputerChoice == "rock";
    

    To assign "rock" to ComputerChoice, make sure you use =:

    ComputerChoice = "rock";
    
  2. You return the computer's choice with return ComputerChoice;, but you never save that returned value when calling GetComputerChoice():

    GetComputerChoice( ComputerChoice );
    

    Instead, you'll have to write it as:

    ComputerChoice = GetComputerChoice();
    

    Notice that you don't need to pass anything into the function; you can remove std::string ComputerChoice as a function parameter.

Of course, the two examples above aren't the only places in your code that're incorrect; you'll have to go through and fix any part of your code that hits one of the above issues.

Upvotes: 1

Related Questions