I have it all written, and it works. However, I have to enter a value and it brings the prompt up again. When I enter the value in the second time, it plays the game. I always have to enter the value twice to get it to work. I'm not sure what is going wrong.
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int computerChoice()
{
int compChoice = rand() % 3;
return compChoice;
}
char humanChoice()
{
char choice;
while(true)
{
// prompt for, and read, the human's choice
cout << "Choose(Rock,Paper,Scissors,or Quit): ";
cin >> choice;
cin.ignore(1000, 10);
// if human wants to quit, break out of loop
if (choice == 'Q' || 'q') break;
if (choice == 'R' || choice == 'r') break;
if (choice == 'S' || choice == 's') break;
if (choice == 'P' || choice == 'p') break;
}
return choice;
}
void printScore()
{
char human;
int computer;
human = humanChoice();
computer = computerChoice();
// human choice
if (human == 'R' || human == 'r')
cout << "Human: R, ";
else if (human == 'S' || human == 's')
cout << "Human: S, ";
else if (human == 'P' || human == 'p')
cout << "Human: P, ";
else cout << "Invalid Choice" << endl;
// computer choice
if (computer == 0)
cout << "Computer: R, ";
else if (computer == 1)
cout << "Computer: P, ";
else if (computer == 2)
cout << "Computer: S, ";
// determine winner
if (computer == 0 && human == 'R')
cout << "Tie" << endl;
else if (computer == 0 && human == 'r')
cout << "Tie" << endl;
else if (computer == 0 && human == 'P')
cout << "Human wins" << endl;
else if (computer == 0 && human == 'p')
cout << "Human wins" << endl;
else if (computer == 0 && human == 'S')
cout << "Computer wins" << endl;
else if (computer == 0 && human == 's')
cout << "Computer wins" << endl;
else if (computer == 1 && human == 'P')
cout << "Tie" << endl;
else if (computer == 1 && human == 'p')
cout << "Tie" << endl;
else if (computer == 1 && human == 'R')
cout << "Computer wins" << endl;
else if (computer == 1 && human == 'r')
cout << "Computer wins" << endl;
else if (computer == 1 && human == 'S')
cout << "Human wins" << endl;
else if (computer == 1 && human == 's')
cout << "Human wins" << endl;
else if (computer == 2 && human == 'S')
cout << "Tie" << endl;
else if (computer == 2 && human == 's')
cout << "Tie" << endl;
else if (computer == 2 && human == 'R')
cout << "Human wins" << endl;
else if (computer == 2 && human == 'r')
cout << "Human wins" << endl;
else if (computer == 2 && human == 'P')
cout << "Computer wins" << endl;
else if (computer == 2 && human == 'p')
cout << "Computer wins" << endl;
}
int main()
{
// initialize the computer's random number generator
srand(time(0));
// declare variables
char human;
int computer;
// start loop
while (true)
{
human = humanChoice();
computer = computerChoice();
if (human == 'Q' || human == 'q') break;
// print results
printScore();
}
// end loop
return 0;
}