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

これはこれまでの私のコードです。私がやろうとしているのは、関数を使用してユーザーの入力 (int) を印刷用の文字列に変換することです。私の現在のコードがコンパイラエラーを引き起こしている理由を誰か説明できますか? エラーが私に伝えていることは次のとおりです。Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)明確にするために、これはインストラクターが私たちにプログラムを作成することを望んでいる方法です。ユーザーの入力を単純に文字列として受け入れることはできません (プログラムの後半で値の比較を行う必要があり、文字列を比較する方法はまだわかりません)。前もって感謝します。

4

3 に答える 3

1

追加する必要があります#include <string>

于 2013-10-19T05:12:57.253 に答える
0

cout << "あなたの選択: " << convertToString(playerChoice) << endl;

convertToString(playerChoice) は文字列型を返します。含めないと cout<<(string type) できません

于 2013-10-19T07:01:59.660 に答える