Hello, I am new to programming and need help with an oldie. I have read many other posts about the Rock-Paper-Scissors program, but cannot locate an answer for my difficulty...
I am using Visual Studio 2017. My code compiles and runs, but does not run properly passed my Input Validation. The goal is to create and call functions for the different steps of the program.
Lines 64-85 were given, except I renamed the variable on lines 84 and 87 to compChoice.
Any insight or suggestions would be appreciated. Thank you in advance.
The problem is on line 33. You never set the compChoice variable in the main() function to the value returned by getComputerChoice(). It should be this:
#include <iostream>
#include <ctime>
#include <cmath>
usingnamespace std;
// Function Prototypes.
int getComputerChoice();
void compChooseRock();
void compChoosePaper();
void compChooseScissors();
void noWinner();
void validateEntry();
// Declare constants (constexpr is supported for compile time constants since C++11)
constexprint ROCK = 1;
constexprint PAPER = 2;
constexprint SCISSORS = 3;
// Declare variables.
int userChoice, compChoice;
int main()
{
// Display instructions to user.
cout << " I challenge you to Rock-Paper-Scissors! \n\n";
cout << " Select Rock, Paper, or Scissors. \n\n";
cout << " Press 1 for Rock\n\t 2 for Paper\n\t 3 for Scissors\n\n";
cout << " Choose wisely...\n\n";
cout << " Enter your selection: ";
cin >> userChoice;
// Call computer's choice.
compChoice = getComputerChoice();
// User input validation.
if (userChoice != 1 && userChoice != 2 && userChoice != 3)
validateEntry();
switch (compChoice)
{
case ROCK: compChooseRock(); break;
case PAPER: compChoosePaper(); break;
case SCISSORS: compChooseScissors(); break;
default: noWinner();
}
system("pause");
return 0;
}
// ********************************************************
// The getComputerChoice function returns the computer's *
// game choice. It returns 1 for rock (via the ROCK *
// constant), or 2 for paper (via the PAPER constant), *
// or 3 for scissors (via the SCISSORS constant). *
// ********************************************************
int getComputerChoice()
{
// Get the system time so we can use it
// to seed the random number generator.
unsigned seed = time(0);
// Use the seed value to seed the random
// number generator.
srand(seed);
// Generate a random number in the range of 1-3.
int compChoice = 1 + rand() % 3;
// Return the random value.
return compChoice;
}
// Function for compChioce = 1.
void compChooseRock()
{
if (userChoice == SCISSORS)
{
cout << " I chose Rock" << endl;
cout << " You chose Scissors" << endl;
cout << " Rock breaks Scissors\n";
cout << " You loose... Better luck next time.\n" << endl;
}
elseif (userChoice == PAPER)
{
cout << " I chose Rock" << endl;
cout << " You chose Paper" << endl;
cout << " Paper covers Rock\n";
cout << " Congratulations! You WIN!\n" << endl;
}
}
// Function for compChoice = 2.
void compChoosePaper()
{
if (userChoice == ROCK)
{
cout << " I chose Paper" << endl;
cout << " You chose Rock" << endl;
cout << " Paper covers Rock\n";
cout << " You loose... Better luck next time.\n" << endl;
}
elseif (userChoice == SCISSORS)
{
cout << " I chose Paper" << endl;
cout << " You chose Scissors" << endl;
cout << " Scissors cut Paper\n";
cout << " Congratulations! You WIN!\n" << endl;
}
}
// Function for compChoice = 3.
void compChooseScissors()
{
if (userChoice == PAPER)
{
cout << " I chose Scissors" << endl;
cout << " You chose Paper" << endl;
cout << " Scissors cut Paper\n";
cout << " You loose... Better luck next time.\n" << endl;
}
elseif (userChoice == ROCK)
{
cout << " I chose Scissors" << endl;
cout << " You chose Rock" << endl;
cout << " Rock breaks Scissors\n";
cout << " Congratulations! You WIN!\n" << endl;
}
}
// Function for compChoice = userChoice.
void noWinner()
{
{
cout << " We chose the same.\n" << endl;
cout << " It is a TIE!" << endl;
}
}
// Function for improper input.
void validateEntry()
{
cout << " Invalid entry. Please try again.\n" << endl;
}
Thank you IceStorm. I need to learn how switch works. That shortened the code considerably. I will study your example. In my code, I changed line 33, made line 41 an if, lines 80 and 83 changed the variable name back to the original designation number, and it ran.
However, (referencing my code) when I run it multiple times, occasionally it hangs up. If my input was 1, 2, 3, 5, e, 1, 2, 3, 8, t, 1, 2, 3, g, it seems to not run the remaining code on one of the valid inputs like the second time I enter 2. Yet works properly on the first and third 2. Any thoughts on why it seems to fail randomly?
Never mind. I found it! After entering 1, 1, 1, .... enough times, I had never reached the noWinner. I placed it as my second if on line 36. My code was never reaching line 56 to check if it was a tie.