There's also an error where you check for "userChoice < 0" meaning that 0 is treated like a valid input, as well as 1,2,3. That wouldn't be so bad if you rewrote the program to tell the player that "0 == end program" and the game keeps playing until you enter a zero for your move. But right now, entering a zero causes the game to just say "you picked 0" and do nothing else. So I guess that "0" is an "undocumented feature" not a bug meaning "I didn't want to play anyway!"
you also set it to loop while there's a tie. It's right in your code, here:
while (userChoice == compChoice){
cout << "You have tied with your opponent. Try again. \n\n\n" << endl << endl << endl;
cin >> userChoice;
compChoice = ((rand() % 3) + 1);
while (userChoice >= 4 || userChoice < 0) {
cout << "Wrong input" << endl;
cin >> userChoice;
}
}
If you don't want it to repeated, don't use a while loop, us some sort of "if" statement.
Here's a compacted version of your code, that doesn't have the looping behaviour and doesn't have repeated sections of code. Also, i added a lookup value for the name of the move, so that you don't have to print out just a number:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
char *choiceName[4] =
{
"",
"Paper",
"Scissors",
"Rock"
};
int main(){
int userChoice, compChoice;
srand(time(0)); //RNG Machine here.
cout << "Welcome to the game of games.\n\n\n";
cout << "1 is for rock, 2 is for paper, and 3 is for scissors. Now pick a number.\n\n\n";
do
{
cin >> userChoice;
if(userChoice < 1 || userChoice > 3)
{
cout << "Have you no shame! You didn't pick 1-3. Try again.\n\n\n";
}
}
while(userChoice < 1 || userChoice > 3);
compChoice = ((rand() % 3) + 1); //RNG's Range.
cout << "You picked: " << choiceName[userChoice] << "\n\n\n";
cout << "Your opponent picked: " << choiceName[compChoice] << "\n\n\n";
if(userChoice == compChoice)
{
cout << "You have tied with your opponent.\n\n\n";
}
else if(userChoice == compChoice - 1 || userChoice == compChoice + 2)
{
cout << "You lose\n\n\n";
}
else // if(compChoice == userChoice - 1 || compChoice == userChoice + 2) // commented out "if" statement, since this "else" will always be called if you don't tie and don't lose, e.g. you won
{
cout << "You win!\n\n\n";
}
system("pause");
return 0;
}