Hello everyone, I am having trouble fine tuning my code. I like to have all the bulky parts of my main in functions, and I can not seem to figure out how to write this code as a function. Anyone tell me where to start?

int main()
{
  int player = 0;
  int computer = 0;
  int tie = 0;
  do {
    char userChoice;
    char compChoice;
    compChoice = getCompChoice();
    userChoice = getUserChoice();

    cout << "User picked: " << userChoice << endl;
    cout << "Computer picked: " << compChoice << endl;


    if (compChoice == userChoice) {
      cout << "It's a tie.\n";
      tie += 1;
    } else if ((compChoice == 'R') && (userChoice == 'S')) {
      cout << "Rock smashes scissors. You Lose.\n";
      computer += 1;
    } else if ((compChoice == 'P') && (userChoice == 'R')) {
      cout << "Paper covors rock. You Lose.\n";
      computer += 1;
    } else if ((compChoice == 'S') && (userChoice == 'P')) {
      cout << "Scissors cut paper. You Lose.\n";
      computer += 1;
    } else if ((compChoice == 'R') && (userChoice == 'P')) {
      cout << "Paper covors rock. You Win.\n";
      player += 1;
    } else if ((compChoice == 'P') && (userChoice == 'S')) {
      cout << "Scissors cut paper. You Win.\n";
      player += 1;
    } else if ((compChoice == 'S') && (userChoice == 'R')) {
      cout << "Rock smashes scissors. You Win.\n";
      player += 1;
    }
    cout << "User: " << player << endl
         << "Computer: " << computer << endl
         << "Ties: " << tie << endl;
  } while (doItAgain());
  return (0);
}

Thats my main this is the code I would like to be a function.

if (compChoice == userChoice) {
      cout << "It's a tie.\n";
      tie += 1;
    } else if ((compChoice == 'R') && (userChoice == 'S')) {
      cout << "Rock smashes scissors. You Lose.\n";
      computer += 1;
    } else if ((compChoice == 'P') && (userChoice == 'R')) {
      cout << "Paper covors rock. You Lose.\n";
      computer += 1;
    } else if ((compChoice == 'S') && (userChoice == 'P')) {
      cout << "Scissors cut paper. You Lose.\n";
      computer += 1;
    } else if ((compChoice == 'R') && (userChoice == 'P')) {
      cout << "Paper covors rock. You Win.\n";
      player += 1;
    } else if ((compChoice == 'P') && (userChoice == 'S')) {
      cout << "Scissors cut paper. You Win.\n";
      player += 1;
    } else if ((compChoice == 'S') && (userChoice == 'R')) {
      cout << "Rock smashes scissors. You Win.\n";
      player += 1;
    }

Recommended Answers

All 7 Replies

create the function and pass the three variables (tie, player and computer) by reference to it.

ok that is what I was wondering. But is it pssible to pass chars and int&

char result(char compChoice, char userChoice, int& player, int& computer, int& tie) {

  if (compChoice == userChoice) {
    tie += 1;
    return ("It's a tie.\n")
  } else if ((compChoice == 'R') && (userChoice == 'S')) {
    computer += 1;
    return ("Rock smashes scissors. You Lose.\n")
  } else if ((compChoice == 'P') && (userChoice == 'R')) {
    computer += 1;
    return ("Paper covors rock. You Lose.\n")
  } else if ((compChoice == 'S') && (userChoice == 'P')) {
    computer += 1;
    return ("Scissors cut paper. You Lose.\n")
  } else if ((compChoice == 'R') && (userChoice == 'P')) {
    player += 1;
    return ("Paper covors rock. You Win.\n")
  } else if ((compChoice == 'P') && (userChoice == 'S')) {
    player += 1;
    return ("Scissors cut paper. You Win.\n")
  } else if ((compChoice == 'S') && (userChoice == 'R')) {
    player += 1;
    return ("Rock smashes scissors. You Win.\n")
  }
}

I am honestly not trying to get you to write my code, just a lil help where to go from here.

looks ok to me except the function should return char* not char. See lines 13 and 14 below.

The rest is simple

int main()
{
  int player = 0;
  int computer = 0;
  int tie = 0;
  do {
    char userChoice;
    char compChoice;
    const char* msg;
    compChoice = getCompChoice();
    userChoice = getUserChoice();

    msg = result(compChoice, userChoice, player, computer, tie);
    cout << msg << "\n";

    cout << "User picked: " << userChoice << endl;
    cout << "Computer picked: " << compChoice << endl;

    
    cout << "User: " << player << endl
         << "Computer: " << computer << endl
         << "Ties: " << tie << endl;
  } while (doItAgain());
  return (0);
}

What does that mean? return a void not a char, does that mean it should be void result(char, char, int&, int&, int&) ?

What does that mean? return a void not a char, does that mean it should be void result(char, char, int&, int&, int&) ?

Please re-read my post -- I realized void was not right and made appropriate correction. Sorry if I confused you. :$

Still got a bunch of errors, but I think I can take it from here. Thanks for all your help :)

It is much better if you use a switch rather that if statement.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.