Hello. I have been out of programming in C++ for about a year now (around the time I took my last C++ class) and I am wanting to get back into it. So I decided to make a rock, paper, scissors game to refresh my memory but I am running into some problems on finalizing my program.
int main()
{
bool quit = false;
displayMenu();
while (quit != true)
{
int userInput = 0;
string userResult = "";
cout << "Press 1 for Rock, 2 for Paper, 3 for Scissors, or '0' to quit: ";
cin >> userInput;
if (userInput == 0)
{
quit = true;
break;
}
if (userInput == 1 | userInput == 2 | userInput == 3)
{
userResult = userSelection(userInput);
compareChoice(userResult);
}
else
{
cout << endl;
cout << "Please enter 1, 2, 3, or '0' to quit." << endl;
cout << endl;
}
}
return 0;
}
Above is my first problem. I want to make sure the user only inputs numbers 1, 2, 3, or 0. How do I go about detecting when the user enters anything but these numbers? I have tried functions (isdigit()) as well as using Try/Catch.
My second problem is that the game's logic is not working properly. For the first few choices, it will determine who wins correctly but after a few rounds, it starts giving incorrect logic (i.e. Rock beats Paper). I know Im not suppose to submit a lot of code but I think the only way for someone to understand my problem is by posting the rest of the code:
#include <iostream>
#include <string>
#include <ctime>
#include <cmath>
#include <windows.h>
using namespace std;
void displayMenu()
{
cout << "\t\tWelcome to my Rock, Paper, Scissors Game!\n" << endl;
}
string computerSelection()
{
int randomNum = 0;
string computerChoice = "";
srand((unsigned)time(0));
randomNum = (rand() % 3) + 1;
if (randomNum ==1)
{
computerChoice = "Rock.";
}
else if (randomNum == 2)
{
computerChoice = "Paper.";
}
else
{
computerChoice = "Scissors.";
}
return computerChoice;
}
string userSelection (int userInput)
{
string userChoice = "";
cout << endl;
if (userInput == 1)
{
userChoice = "Rock.";
cout << "You're Choice: " << userChoice << endl;
Sleep(1000);
cout << endl;
cout << "Computer's Choice: " << computerSelection() << endl;
Sleep(1000);
}
else if (userInput == 2)
{
userChoice = "Paper.";
cout << "You're Choice: " << userChoice << endl;
Sleep(1000);
cout << endl;
cout << "Computer's Choice: " << computerSelection() << endl;
Sleep(1000);
}
else
{
userChoice = "Scissors.";
cout << "You're Choice: " << userChoice << endl;
Sleep(1000);
cout << endl;
cout << "Computer's Choice: " << computerSelection() << endl;
Sleep(1000);
}
return userChoice;
}
void compareChoice(string userResult)
{
string computerResult = "";
computerResult = computerSelection();
cout << endl;
if (userResult == computerResult)
{
cout << "Tie, you both picked " << userResult;
Sleep(1000);
}
else if (userResult == "Rock." & computerResult == "Scissors.")
{
cout << "Congrats! You Win!";
Sleep(1000);
}
else if (userResult == "Paper." & computerResult == "Rock.")
{
cout << "Congrats! You Win!";
Sleep(1000);
}
else if (userResult == "Scissors." & computerResult == "Paper.")
{
cout << "Congrats! You Win!";
Sleep(1000);
}
else
{
cout << "Sorry, you lose!";
Sleep(1000);
}
cout << "\n\n";
}
Any help, suggestions, and comments are greatly appreciated.