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;
}

You're asking the player for his choice (by calling humanChoice) twice: Once in main and once in printScore.

PS: On line 24 you will always break because the condition choice == 'Q' || 'q' is always true (because 'q' is true).

Oh, wow. That fixed that issue. Thanks.
Because I have

choice == 'Q' || 'q' break;

Is that what is causing it so my program can not exit with q?

No, it just means that humanChoice will return even if the user enters invalid input. Qutting with 'q' shoudl work fine (at least I can't see why it wouldn't at the moment).

I took the humanChoice out of main, and left it in printScore. However, now when I enter q it says invalid choice.

That's because your code in printScore doesn't check whether the user entered 'q'. Only your code in main did. If you handle the user input in printScore, you need to move the code that handles the user entering 'q' in printScore as well.

In my printScore, I have to check for q? Or can I just move all the user input into the humanChoice function.

If you take humanChoice out of main then it will never know what the human variable is in main for the check:

if (human == 'Q' || human == 'q') break;

so it will never exit when you press Q or q. And if you take humanChoice out of printScore it will not know what the human variable is in printScore() unless you pass it in to the print score function like printScore(human).

So what I would change is:

void printScore()

to

void printScore(char human)

remove

char human;

and

human = humanChoice();

from the print score function

re add

human = humanChoice();

to your main() and then change your printScore function call to pass in the human variable from main like this

printScore(human);

Here is the full source code with changes and a few comment lines above the changed lines to show you where things were edited:

#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;
    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(human);
    }
    // end loop

    return 0;
}
Comments
gj bro

Thank you! That fixes that, however I rewrote my the coding, and now have a new problem. I haven't added the loops in yet, but I will. It compiles and runs fine. However, it only prints the human and computer choices, not the outcome. I can't figure out why.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int userChoice()
{
  char choice;
  int choice2;

  do
  {
    cout << "Please enter your choice[r,p,s,q]: ";
    cin >> choice;
    cout << "Human: " << choice;

  }while ((choice != 'R')&&(choice != 'r')&&(choice != 'P')&&(choice != 'p')&&(choice != 'S')&&(choice != 's'));

  if(choice == 'R' || choice == 'r')
    choice2 = 0;
  if(choice == 'P' || choice == 'p')
    choice2 = 1;
  if (choice == 'S' || choice == 's')
      choice2 = 2;
  if (choice == 'Q' || choice == 'q')
    choice2 = 3;

  return choice2;
}

int computerChoice()
{
  srand(time(0));
  char compChoice = rand() % 3;

  if (compChoice == 0)
    cout << " Computer: R" << endl;
  if (compChoice == 1)
    cout << " Computer: P" << endl;
  if (compChoice == 2)
    cout << " Computer: S" << endl;

  return compChoice;
}

int printOutcome(int choice, int compChoice)
{
  if (choice == '0' && compChoice == '0')
    cout << "Tie \n";
  if (choice == '0' && compChoice == '1')
    cout << "Computer wins\n";
  if (choice == '0' && compChoice == '2')
    cout << "Human wins\n";
  if (choice == '1' && compChoice == '0')
    cout << "Human wins\n";
  if (choice == '1' && compChoice == '1')
    cout << "Tie\n";
  if (choice == '1' && compChoice == '2')
    cout << "Computer wins\n";
  if (choice == '2' && compChoice == '0')
    cout << "Computer wins\n";
  if (choice == '2' && compChoice == '1')
    cout << "Human wins\n";
  if (choice == '2' && compChoice == '2')
    cout << "Tie\n";

  cout << endl;
}

int main()
{
  int humanChoice;
  int compChoice;
  int printScore;

  humanChoice = userChoice();
  compChoice = computerChoice();
  printScore = printOutcome(humanChoice, compChoice);

  return 0;
}

You could eliminate a certain amount of redundancy by using toupper() (defined in the <cctype> header) to force the user input to upper case:

int userChoice()
{
    char choice;
    int choice2;
    do
    {
        cout << "Please enter your choice[r,p,s,q]: ";
        cin >> choice;
        choice = toupper(choice);
        cout << "Human: " << choice;
    }while ((choice != 'R')&&(choice != 'P')&&(choice != 'S'));

    if(choice == 'R')
        choice2 = 0;
    if(choice == 'P')
        choice2 = 1;
    if (choice == 'S')
        choice2 = 2;
    if (choice == 'Q')
        choice2 = 3;

    return choice2;
}

It's a small change, but one which makes the function quite a bit easier to read.

Edited 4 Years Ago by Schol-R-LEA

Thanks, it does make it a lot easier to read. Still need help with why it won't print the outcome though.

Hmmn, fixated on the first part as I was, I missed the whole question. Ah, yes, here's the problem: you are putting the numbers in quotes, which means that you are comparing the input values (0, 1, 2, 3) against their character representations ('0', '1', '2', '3'). While those representations are indeed treated as valid int values, they are not the same values as the integers. For example, the character '0' is actually encoded as the numeric value 48. Thus, you want to drop the quotes around the numbers, like so:

int printOutcome(int choice, int compChoice)
{
    if (choice == 0 && compChoice == 0)
        cout << "Tie \n";
    if (choice == 0 && compChoice == 1)
        cout << "Computer wins\n";
    if (choice == 0 && compChoice == 2)
        cout << "Human wins\n";
    if (choice == 1 && compChoice == 0)
        cout << "Human wins\n";
    if (choice == 1 && compChoice == 1)
        cout << "Tie\n";
    if (choice == 1 && compChoice == 2)
        cout << "Computer wins\n";
    if (choice == 2 && compChoice == 0)
        cout << "Computer wins\n";
    if (choice == 2 && compChoice == 1)
        cout << "Human wins\n";
    if (choice == 2 && compChoice == 2)
        cout << "Tie\n";
    cout << endl;
}

This has the values compared to the equivalent integer values, not the character encodings.

I didn't even realize I was doing it that way. Thank you, my program is working now.

The code that you are using for checking who wins is pretty bulky. If you just treat the hands like 0, 1 and 2 and that higher numbers beat lower numbers with the exception that 0 beats 2 then you can just use the mod operator to wrap around and see who wins.

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
    srand(time(NULL));
    int player, cpu;
    char option;
    int wins = 0, losses = 0, ties = 0;

    char hands[3] = {'r', 'p', 's'};

    while(1)
    {
        cout << "Enter (R)ock, (P)aper, (S)cissors, or (Q)uit:" << endl;
        cin >> option;

        if( tolower(option) == 'r' )
            player = 0;
        else if( tolower(option) == 'p' )
            player = 1;
        else if( tolower(option) == 's' )
            player = 2;
        else if( tolower(option) == 'q' )
            break;
        else
            continue;

        cpu = rand()%3;

        cout << "Player | CPU" << endl;
        cout << hands[player] << "    " << hands[cpu] << endl << endl;


        if( player == cpu )
        {
            cout << "Tie!" << endl;
            ties++;
        }
        else if( player == (cpu+1)%3 )
        {
            cout << "Player wins!" << endl;
            wins++;
        }
        else
        {
            cout << "CPU wins!" << endl;
            losses++;
        }

        cout << "Score:" << endl;
        cout << "w: " << wins << " | l: " << losses << " | t: " << ties << endl << endl;
    }

    return 0;
}

I added in a score keeping feature too.

That makes it much cleaner, however I had to do it a certain way, but thank you!

This question has already been answered. Start a new discussion instead.