当我打电话给我的功能时,什么都没发生?

1 c++ function

我正在尝试使用两个char参数来制作摇滚,纸张,剪刀游戏的功能,其中第一个参数代表用户选择的摇滚,纸张或剪刀.第二个参数代表游戏的结果,胜利,失败或平局.但是,当我试图调用该函数时,没有任何事情发生.我迷失在接下来我需要做什么.非常感谢所有帮助!

#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!\n\n";

    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();
    }
Run Code Online (Sandbox Code Playgroud)

Asu*_*Asu 6

一般评论

using namespace std;
Run Code Online (Sandbox Code Playgroud)

避免using namespace std.

return main();
Run Code Online (Sandbox Code Playgroud)

您不能拨打电话main.这将导致未定义的行为.另外,你的意图是什么?

rand()
Run Code Online (Sandbox Code Playgroud)

rand()应该避免.这是一个有趣的视频,说明为什么你不应该使用它,而是使用C++ 11 随机.

y = choice2;
Run Code Online (Sandbox Code Playgroud)

您正在传递y值,这意味着分配它不会y从外部修改.在执行此操作时(即在声明中),您应该通过y引用传递char& y.

为什么功能没有做任何事情?

......实际上,确实如此!

user2 == '2'
Run Code Online (Sandbox Code Playgroud)

你的比较被打破了.'2'实际上不是2,但是50.原因是它'2'是一个字符,所以你实际上正在阅读相关的字符代码.

这意味着你的所有条件都是假的playRPS,所以函数只能调用它main()(in 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;
Run Code Online (Sandbox Code Playgroud)

都好!让我们用电脑的选择做同样的事情吧!

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;
Run Code Online (Sandbox Code Playgroud)

然后,您应该比较玩家选择的内容与计算机选择的内容,并告诉谁是赢家.不幸的是,我们不能比较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;
Run Code Online (Sandbox Code Playgroud)

好!现在我们还可以改善第一个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;
Run Code Online (Sandbox Code Playgroud)

这意味着我们也可以比较user1,user2所以我们知道谁赢了.

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;
}
Run Code Online (Sandbox Code Playgroud)

但是,使用1,23没有使事情非常清楚.如果您使用a enum来表示这些值,该怎么办?

enum RPSChoice
{
    ROCK = 1,
    PAPER = 2,
    SCISSORS = 3
};
Run Code Online (Sandbox Code Playgroud)

例如,第一个块现在看起来像:

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;
Run Code Online (Sandbox Code Playgroud)

如果我们将新的两个第一个块包装成一个函数,以便我们避免重复自己怎么办?

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;
}
Run Code Online (Sandbox Code Playgroud)

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

printDecision("You", user1);
printDecision("The computer", user2);
Run Code Online (Sandbox Code Playgroud)

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

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
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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!
}
Run Code Online (Sandbox Code Playgroud)

完成!这是所有改进的最终程序(没有做任何替换rand()),是一个尝试它的在线提示.

请注意,有更多方法可以改进代码,进一步简化代码并使其更清晰.我最值得注意的是std :: unordered_mapRPSChoice值绑定到a string,而a 绑定char到a RPSChoice.在某些情况下,您可能还希望切换if.

正如您对问题的评论所述,您可以使用调试器诊断出此问题.πάνταῥεῖ的评论可供参考:

解决此类问题的正确工具是您的调试器.在询问Stack Overflow之前,您应该逐行浏览代码.有关更多帮助,请阅读如何调试小程序(作者Eric Lippert).

  • 这家伙有太多的空闲时间. (3认同)