0
#include <iostream>
#include <random>
#include <cstdlib>
#include <time.h>

using namespace std;

int getComputerChoice();
int getPlayerChoice();
string convertToString(int);

int main()
{
    int computerChoice, playerChoice;
    string choiceOne, choiceTwo;

    cout << "ROCK PAPER SCISSORS MENU\n"
         << "-------------------------\n"
         << "p) Play Game\n"
         << "q) Quit" << endl;

    srand (time(NULL));

    computerChoice = getComputerChoice();
    playerChoice = getPlayerChoice();

    cout << "You chose: " << convertToString(playerChoice) << endl;
    cout << "The computer chose: " << convertToString(computerChoice) << endl;

    system("PAUSE");
    return 0;
}

int getComputerChoice()
{
    int choiceComp = (rand() % 3) + 1;
    return choiceComp;
}

int getPlayerChoice()
{
    int choicePlayer;

    do {
    cout << "Rock, Paper or Scissors?\n"
         << "1) Rock\n"
         << "2) Paper\n"
         << "3) Scissors\n"
         << "Please enter your choice: " << endl;
    cin >> choicePlayer;
    } while (choicePlayer < 1 || choicePlayer > 3);

    return choicePlayer;
}

string convertToString(int choiceAsInt)
{
    string choiceName;

    if (choiceAsInt == 1)
    {
        choiceName = "Rock";
    }
    else if (choiceAsInt == 2)
    {
        choiceName = "Paper";
    }
    else choiceName = "Scissors";

    return choiceName;
}

This is my code thus far. What I am trying to do is use a function to convert the user's input (which is an int) into a string for printing. Can anyone explain why my current code is causing a compiler error? Here is what the error is telling me: Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) And for clarification, this is how the instructor wants us to create the program. We aren't allowed to simply accept the user's input as a string (later on in the program we have to do comparisons of the values and we don't know how to compare strings yet). Thanks in advance.

3 Answers3

3
#include<string>

In code:do following changes

 cout << "You chose: "<<convertToString(playerChoice).c_str() << endl;
 cout << "The computer chose: "<<convertToString(computerChoice).c_str()<< endl;
MoBaShiR
  • 442
  • 3
  • 12
1

You need to add #include <string>

lolando
  • 1,721
  • 1
  • 18
  • 22
0

cout << "You chose: " << convertToString(playerChoice) << endl;

convertToString(playerChoice) return a string type,if you dont include you can not cout<<(string type)

CY1993
  • 1
  • 1