当我调用函数时,什么都没有发生

Nothing happening when I call my function?

本文关键字:什么 调用 函数      更新时间:2023-10-16

我正在尝试为一款带有两个字符参数的剪刀、石头、布游戏创建一个函数,其中第一个参数代表用户对剪刀、石头、布的选择。第二个参数表示游戏的结果,无论是赢、输还是平。然而,当我试图调用该函数时,什么也没有发生。我不知道下一步该做什么。非常感谢所有的帮助!

#include <iostream>
#include <cstdlib>
using namespace std;
double playRPS (char a, char b);

int main() {
    char letter;
    char result = 0;
    cout << "Welcome to COP3014 ROCK PAPER SCISSORS!nn";
    cout << "Please select: " << endl
         << "Rock(r), Paper(p), or Scissors(s)? " << endl
         << "Or enter q to quit --> ";
    cin >> letter;
    if (letter == 'r' || letter == 'R' || letter ==  'p' || letter == 'P'     || letter == 's' || letter == 'S') {
        playRPS(letter, result);
    }
    else {
        cout << "Please enter r, p, or s" << endl;
    }
    return 0;
}
double playRPS (char x, char y) {
    int choice1 = 0, choice2 = 0, choice3 = 0;
    int user2 = rand() % 3 + 1;
    if (( x == 'r' || x == 'R') && (user2 == '2')) {
       cout << "The computer chose... PAPER!";
       cout << "You chose ROCK!";
       cout << "You LOSE!";
       y = choice2;
       return choice2;
    }
    else if ((x == 'r' || x == 'R') && (user2 == '1')) {
        cout << "The computer chose... ROCK!";
        cout << "You chose ROCK!";
        cout << "You TIED!";
        y = choice3;
        return choice3;
    }
    else if ((x == 'r' || x == 'R') && (user2 == '3')) {
        cout << "The computer chose... SCISSORS!";
        cout << "You chose ROCK!";
        cout << "You WIN!";
        y = choice1;
        return choice1;
    }
    else if (( x == 'p' || x == 'P') && (user2 == '2')) {
        cout << "The computer chose... PAPER!";
        cout << "You chose PAPER!";
        cout << "You TIED!";
        y = choice3;
        return choice3;
    }
    else if (( x == 'p' || x == 'P') && (user2 == '1')) {
        cout << "The computer chose... ROCK!";
        cout << "You chose PAPER!";
        cout << "You WIN!";
        y = choice1;
        return choice1;
    }
    else if (( x == 'p' || x == 'P') && (user2 == '3')) {
        cout << "The computer chose... SCISSORS!";
        cout << "You chose PAPER!";
        cout << "You LOSE!";
        y = choice2;
        return choice2;
    }
    else if (( x == 's' || x == 'S') && (user2 == '2')) {
        cout << "The computer chose... PAPER!";
        cout << "You chose SCISSORS!";
        cout << "You WIN!";
        y = choice1;
        return choice1;
    }
    else if (( x == 's' || x == 'S') && (user2 == '1')) {
        cout << "The computer chose... ROCK!";
        cout << "You chose SCISSORS!";
        cout << "You LOSE!";
        y = choice2;
        return choice2;
    }
    else if (( x == 's' || x == 'S') && (user2 == '3')) {
        cout << "The computer chose... SCISSORS!";
        cout << "You chose SCISSORS!";
        cout << "You TIED!";
        y = choice3;
        return choice3;
    }
    else{
        return main();
    }

总论

using namespace std;

避免using namespace std

return main();

不允许在代码中调用main。这将导致未定义行为。另外,你来这里的目的是什么?

rand()
应避免使用

rand()。这里有一个有趣的视频,告诉你为什么不应该使用它,而应该使用c++ 11。

y = choice2;

您通过值传递y,这意味着分配它不会从外部修改y。你应该通过引用传递y(即声明中的char& y)。

为什么函数不做任何事情?

…事实上,它有!

user2 == '2'

你的比较错了。'2'实际上不是2,而是50。原因是'2'是一个字符,所以您实际上是在读相关的字符代码。

这意味着在playRPS中所有的条件都是假的,所以函数唯一做的就是调用main()(在return main();中)。

如何缩短代码?

您的测试用例是相当冗余和沉重的。你可以改变它来大大减少你的代码大小。

让我们打印播放器选择的选项…

if (x == 'r' || x == 'R')
    cout << "You chose ROCK!" << endl;
else if (x == 'p' || x == 'P')
    cout << "You chose PAPER!" << endl;
else if (x == 's' || x == 'S')
    cout << "You chose SCISSORS!" << endl;

所有的好!让我们对计算机的选择做同样的事情!

if (user2 == 1)
    cout << "The computer chose... ROCK!" << endl;
else if (user2 == 2)
    cout << "The computer chose... PAPER!" << endl;
else if (user2 == 3)
    cout << "The computer chose... SCISSORS!" << endl;

然后你应该比较玩家的选择和电脑的选择,并告诉谁是赢家。不幸的是,我们不能将xuser2进行比较,除非再做很多案例。

如果我们决定将x的选择以与user2相同的方式保存会怎么样?我们还可以使用tolower来避免检查字母的大写变体。

int user1 = 0;
x = tolower(x); // we force x to lower case
if (x == 'r')
    user1 = 1;
else if (x == 'p')
    user1 = 2;
else if (x == 's')
    user1 = 3;

好!现在我们还可以改善第一个if/else if块中的条件:

if (user1 == 1)
    cout << "You chose ROCK!" << endl;
else if (user1 == 2)
    cout << "You chose PAPER!" << endl;
else if (user1 == 3)
    cout << "You chose SCISSORS!" << endl;

这意味着我们也可以比较user1user2,这样我们就知道谁赢了。

if (user1 == user2) {
    cout << "It's a TIE!" << endl;
}
else if ((user1 == 1 && user2 == 2) ||
         (user1 == 2 && user2 == 3) ||
         (user1 == 3 && user2 == 1)) {
    cout << "You LOSE!" << endl;
}
else {
    cout << "You WIN!" << endl;
}

然而,使用1, 23并不能使事情变得非常清楚。如果使用enum来表示这些值呢?

enum RPSChoice
{
    ROCK = 1,
    PAPER = 2,
    SCISSORS = 3
};
例如,第一个块现在看起来像:
if (user1 == ROCK)
    cout << "You chose ROCK!" << endl;
else if (user1 == PAPER)
    cout << "You chose PAPER!" << endl;
else if (user1 == SCISSORS)
    cout << "You chose SCISSORS!" << endl;

如果我们把前面两个新的代码块封装到一个函数中,以避免重复呢?

void printDecision(string who, int choice) {
    cout << who; // no matter what, we will tell who took a decision
    if (choice == ROCK)
        cout << " chose ROCK!" << endl;
    else if (choice == PAPER)
        cout << " chose PAPER!" << endl;
    else if (choice == SCISSORS)
        cout << " chose SCISSORS!" << endl;
}

通过这种方式,我们可以使playRPS更加清晰,通过将两个大块替换为简单的短函数调用:

printDecision("You", user1);
printDecision("The computer", user2);

让我们做另一个简单的函数来决定谁赢了:

int winner(int user1, int user2) {
    if (user1 == user2) {
        return 0; // tie
    }
    else if ((user1 == ROCK && user2 == PAPER) ||
             (user1 == PAPER && user2 == SCISSORS) ||
             (user1 == SCISSORS && user2 == ROCK)) {
        return 2; // user2 is the winner
    }
    else {
        return 1; // user1 is the winner
    }
}

最后一个返回我们根据给定字符给出的值:

int characterToChoice(char c)
{
    c = tolower(c);
    if (c == 'r')
        return ROCK;
    else if (c == 's')
        return SCISSORS;
    else if (c == 'p')
        return PAPER;
    else
        return 0; // Not a proper choice!
}

完成了!这是所有改进的最终程序(没有做任何事情来取代rand() in),这里是一个在线提示来尝试它。

请注意,有更多的方法可以改进代码,甚至简化它,使其更清晰。我最值得注意的是考虑std::unordered_map将RPSChoice值绑定到string,将char绑定到RPSChoice。在某些情况下,您也可以选择切换到if

正如您的问题的注释所述,您可以使用调试器诊断此问题。π α ντα ρ ε ν的注释供参考:

解决这类问题的正确工具是调试器。在询问Stack Overflow之前,您应该逐行检查代码。更多的帮助,请阅读如何调试小程序(由Eric Lippert)。