【问题标题】:C++ Functions and strange errorsC++ 函数和奇怪的错误
【发布时间】:2016-09-28 18:00:32
【问题描述】:

所以我正在用 C++ 编写一个小型的石头剪刀布游戏结构,但我遇到了一些我不理解的错误。

解决方案

函数 string numberToWord (int x) 不能在函数 main 中。由于编译器的工作方式,它必须是一个单独的方法。我只是将它移出然后它工作正常。

上一个问题

所以我正在用 C++ 编写一个小型的石头剪刀布游戏结构,但我遇到了一些我不理解的错误。

首先是代码需要一个';'在 NumberToWord 函数中,但它不应该,因为它是一个函数。

另一个错误是随机出现的它似乎不喜欢的 else 语句之一。

也许我遗漏了一些东西,我不知道,但这应该是一个简单的修复。

#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

int main()
{
    int seed = static_cast <int> (time(0)); //Sets the random seed
    srand(seed);

    int winCount = 0;

    string numberToWord (int x) {
        string outputChoice;

        if (x == 0) { outputChoice = "Rock"; }
        else if (x == 1) { outputChoice = "Paper"; }
        else if (x == 2) { outputChoice = "Scissors"; }

        return outputChoice;
    }

    while (winCount < 3) {
        int computerChoice = rand() % 4;
        int userChoice;

        cout << userChoice << endl;

        cout << "Please Enter 0 for Rock, 1 for Paper, or 2 for Scissors: "; //Asks for user input
        cin >> userChoice; //Inputs user input to variable

        if (userChoice == computerChoice) {
            cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
            cout << "You Choose: " << numberToWord(userChoice) << endl;
            cout << "Draw!" << endl;
        }
        else if ((userChoice == 1) && (computerChoice == 2)) { //Rock v Paper
            cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
            cout << "You Choose: " << numberToWord(userChoice) << endl;
            cout << "Compuer wins!" << endl;
        }
        else if ((userChoice == 1) && (computerChoice == 3)) { //Rock v Scissors
            cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
            cout << "You Choose: " << numberToWord(userChoice) << endl;
            cout << "You win!" << endl;
            winCount += 1;
        }
        else if ((userChoice == 2) && (computerChoice == 1)) { //Paper v Rock
            cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
            cout << "You Choose: " << numberToWord(userChoice) << endl;
            cout << "You win!" << endl;
            winCount += 1;
        }
        else if ((userChoice == 2) && (computerChoice == 3)) { //Paper v Scissors
            cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
            cout << "You Choose: " << numberToWord(userChoice) << endl;
            cout << "Compuer wins!" << endl;
        }
        else if ((userChoice == 3) && (computerChoice == 1)) { //Scissors v Rock
            cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
            cout << "You Choose: " << numberToWord(userChoice) << endl;
            cout << "Compuer wins!" << endl;
        }
        else if ((userChoice == 3) && (computerChoice == 2)) { //Scissors v Paper
            cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
            cout << "You Choose: " << numberToWord(userChoice) << endl;
            cout << "You win!" << endl;
            winCount += 1;
        }
    }


    return 0;
}

感谢您的所有帮助!


第 2 部分

简单地说程序不喜欢'

参考资料:

http://www.cplusplus.com/doc/tutorial/basic_io/

http://www.cplusplus.com/doc/tutorial/variables/

void displayOutput(int comp, int user, string winner) {
    string compOutputChoice = "";
    string userOutputChoice = "";

    /*
    if (comp == 0) { compOutputChoice = "Rock"; }
    else if (comp == 1) { compOutputChoice = "Paper"; }
    else if (comp == 2) { compOutputChoice = "Scissors"; }

    if (user == 0) { userOutputChoice = "Rock"; }
    else if (user == 1) { userOutputChoice = "Paper"; }
    else if (user == 2) { userOutputChoice = "Scissors"; }
    */

    cout << "Compuer Choose: " << compOutputChoice << endl;
    cout << "You Choose: " << userOutputChoice << endl;
    //cout << winner << endl;

    return;
}

错误:

错误(活动)没有操作符“

错误(活动)没有运算符“

错误 C2679 二进制“

错误 C2679 二进制“

【问题讨论】:

  • 您应该在问题中逐字发布错误消息 - 作为文本不是图像。
  • 你不能在 main() 函数体内定义另一个函数,除非它是一个 lambda 表达式。
  • 解决了一个问题,但是我仍然不能使用符合cout >>的函数来输出它
  • @Rext &gt;&gt; 用于输入,您可能的意思是&lt;&lt; 用于输出 ;-)
  • 您要求基于 0 的输入并计算它们,就好像它们是基于 1 的一样。使用 rand() % 4 是错误的,因为它给你留下了 4 个选项,而不是三个......

标签: c++ debugging compiler-errors logic


【解决方案1】:

函数string numberToWord (int x) 嵌套在main 函数内。那不是有效的 C++。

GCC 编译器确实支持嵌套函数作为扩展,但它不是标准的一部分,其他编译器(据我所知)不接受它。只是不要那样做。将函数移出 main(或者,如果有意义,将其设为 lambda)。

【讨论】:

    【解决方案2】:

    问题很简单。 numberToWord 不能是 main 的内部函数。如果您使用的是较新的 C++,请将其移到 main 之外或将其更改为 lambda。

    auto numberToWord = [](int x) -> string {
        string outputChoice;
    
        if (x == 0) { outputChoice = "Rock"; }
        else if (x == 1) { outputChoice = "Paper"; }
        else if (x == 2) { outputChoice = "Scissors"; }
    
        return outputChoice;
    };
    

    【讨论】:

    • 现在唯一的问题是我还是不能使用:
    • cout
    • @rext,“仍然无法使用”是什么意思。什么不适合你?
    • 很抱歉我是这个网站的新手,所以我的格式很糟糕,我编辑了我的帖子及其在第 2 部分下,希望这是有道理的
    猜你喜欢
    • 2012-07-01
    • 1970-01-01
    • 2014-06-30
    • 1970-01-01
    • 2017-11-11
    • 2016-02-02
    • 2013-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多