2017-10-29 6 views
0

現在ロック、ペーパー、はさみプログラムで作業しています。 getComputerChoice、getUserChoice、displayChoice、およびdecideWinnerの4つの関数を含める必要があります。私は現在displayChoiceに固執していますが、私は文字列を使ってユーザが選択した "Weapon"を表示すると思っていましたが、動作していません。どんな助力/フィードバックも大変ありがとうございます。ありがとうございます。文字列が関数内に印刷されていません

#include <iostream> 
#include <cstdlib> 
#include <time.h> 
#include <string> 
using namespace std; 


void getComputerChoice(int& computerChoice); 
int getUserChoice(int userChoice); 
void displayChoice(int userChoice, int computerChoice); 

int main() 
{ 
    int userChoice = 0, computerChoice; 

    getUserChoice(userChoice); 
    getComputerChoice(computerChoice); 
    displayChoice(userChoice, computerChoice); 


    return 0; 
} 

int getUserChoice(int userChoice) 
{ 
    cout << "Game Menu\n" 
    << "-----------\n" 
    << "1. Rock\n" 
    << "2. Paper\n" 
    << "3. Scissors\n" 
    << "4. Quit\n" 
    << "Enter your choice (1-4):"; 
    cin >> userChoice; 
    return (userChoice); 
} 

void getComputerChoice(int& computerChoice) 
{ 
    srand(time(NULL)); 
    computerChoice = (rand() % 3) + 1; 
} 

void displayChoice(int userChoice, int computerChoice) 
{ 
    string uChoice; 
    if (userChoice == 1) 
     uChoice = "Rock"; 
    else if (userChoice == 2) 
     uChoice = "Paper"; 
    else if (userChoice == 3) 
     uChoice = "Scissors"; 

    cout << "You selected :" << uChoice << endl; 
    cout << "The computer selected :" << computerChoice << endl; 
} 
+1

'userChoice'は常に' getUserChoice'から値を取得しません。 – George

+0

は 'userChoice = getUserChoice(userChoice)'を実行することで修正されました。ありがとう、トン! – Ivan

+0

また、あなたは 'getUserChoice(int)'のためのパラメータを必要としません、とにかく入力を上書きするだけで、あなたは参照渡しでもありません。これを 'userChoice = getUserChoice()'にすることもできます。これは同じように動作します。関数定義を更新するようにしてください。 –

答えて

0

あなたの問題は、スコープの一つであり、あなたは、単にユーザーが入力として提供するもので、このために別の変数を使用する必要がありますが、関数を渡す値を戻していません。あなたのget関数はパラメータを必要とせず、getComputerChoice関数はint型でなければなりません。これらの変更により正しい出力が得られるはずです。

#include <iostream> 
#include <cstdlib> 
#include <time.h> 
#include <string> 
using namespace std; 


int getComputerChoice(); 
int getUserChoice(); 
void displayChoice(int userChoice, int computerChoice); 

int main() 
{ 
int userChoice = 5; 
int computerChoice= 5; 

userChoice = getUserChoice(); 
computerChoice = getComputerChoice(); 
displayChoice(userChoice, computerChoice); 


return 0; 
} 

int getUserChoice() 
{ 
    int selection=5; 
    cout << "Game Menu\n" 
    << "-----------\n" 
    << "1. Rock\n" 
    << "2. Paper\n" 
    << "3. Scissors\n" 
    << "4. Quit\n" 
    << "Enter your choice (1-4):"; 
    cin >> selection; 
    return selection; 
} 

int getComputerChoice() 
{ 
    srand(time(NULL)); 
    return (rand() % 3) + 1; 
} 

void displayChoice(int userChoice, int computerChoice) 
{ 
    string uChoice; 
    if (userChoice == 1) 
     uChoice = "Rock"; 
    else if (userChoice == 2) 
     uChoice = "Paper"; 
    else if (userChoice == 3) 
     uChoice = "Scissors"; 

    cout << "You selected : " << uChoice << endl; 
    cout << "The computer selected : " << computerChoice << endl; 
} 
関連する問題