提问者:小点点

值返回函数


我一直在做这个岩石剪刀剪刀程序一段时间,它没有显示的方式,它应该是应该的。我在代码中使用了值返回函数。问题在程序的末尾显现出来。游戏的结果将被替换,但实际显示的是另一个提示,供用户输入他们的选择。下面是我的代码:


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

using namespace std;

int random();
string input();
string compChoice();
void displayCompChoice(string);
bool compare(string, string);

int main() {
 cout << "\nThis program lets you play the popular Rock-Paper-Scissors Game with your computer! \n\n";
 random(); // computer starts choosing a number from 1 to 3
 cout << "----- ----- ----- ----- -----\n";
 cout << "\nTo play, use your keyboard to enter your choice.\n";
 cout << "Please enter [ rock or paper or scissors ] \n\n";
 cout << "----- ----- ----- ----- -----\n\n";
 
 input(); // function for input of user's choice
 cout << "Computer's choice: ";
 displayCompChoice(compChoice()); // function for the display of the computer's choice
 compare(input(), compChoice());
}

int random() {
    srand(time(NULL));
        int num;
        num = rand() % 3 + 1; //generate random number from 1-3 
}
string input () { 
    string userChoice;
        cout << "Enter your choice: ";
        cin >> userChoice;
    return userChoice; 
    userChoice = input();
}

string compChoice () {
        
        if (random() == 1) 
            return "rock";
        else if (random() == 2)
            return "paper";
        else if (random() == 3) 
            return "scissors"; 
        
}

void displayCompChoice (string) {
    cout << compChoice();
    cout << "\n\n";
    cout << "----- ----- ----- ----- -----\n\n";
}

bool compare (string, string) { 
    if ("rock", "scissors") {
        return true;
        cout << "You win! \nRock smashes scissors.";
    }
    else if ("scissors", "rock") {
        return true; 
        cout << "Computer wins! \nRock smashes scissors.";
    }
    else if ("scissors", "paper") {
        return true;
        cout << "You win! \nScissors cuts paper.";
    }
    else if ("paper", "scissors") {
        return true;
        cout << "Computer wins! \nScissors cuts paper";
   }
    else if ("paper", "rock") {
        return true;
        cout << "You win! \nPaper wraps rock.";
    }
    else if ("rock", "paper") {
        return true;
        cout << "Computer wins! \nPaper wraps rock.";
    }
    else {
        return false;
        cout << "It's a tie! \nPlease try again. \n\n----- ----- ----- ----- -----";
    }
}


共1个答案

匿名用户

您正在两次调用input(),因此提示将出现两次。

相反,你应该

  1. 调用输入()一次
  2. 将返回的值存储到变量
  3. 使用该变量以备以后使用

可以这样做:

int main() {
 cout << "\nThis program lets you play the popular Rock-Paper-Scissors Game with your computer! \n\n";
 random(); // computer starts choosing a number from 1 to 3
 cout << "----- ----- ----- ----- -----\n";
 cout << "\nTo play, use your keyboard to enter your choice.\n";
 cout << "Please enter [ rock or paper or scissors ] \n\n";
 cout << "----- ----- ----- ----- -----\n\n";
 
 string user_hand = input(); // function for input of user's choice
 string computer_hand = compChoice();
 cout << "Computer's choice: ";
 displayCompChoice(computer_hand); // function for the display of the computer's choice
 compare(user_hand, computer_hand);
}

还要注意

  • return语句使其在该点处停止函数的执行,因此compare函数中的cout语句不会被执行。
  • compare语句中使用了
  • 逗号运算符。它的求值结果将是右操作数的结果。您应该:
    • 为参数命名。
    • 将字符串与参数进行比较。相等比较运算符==和逻辑与运算符&将很有用。