【问题标题】:function prototype uninitialized local variable函数原型未初始化的局部变量
【发布时间】:2018-05-08 14:29:55
【问题描述】:

我是一名高中生,对于我的一个期末项目,我的作业包括函数原型。我将包含下面的代码,但始终显示的错误是“使用了未初始化的局部变量“名称””。我在一个单独的函数中定义该变量并将其返回,但它不会返回到 int main。我敢肯定这是显而易见的,但如果有人可以帮助我,我将非常感激。 谢谢

    // Rock, Paper, Scissors Game
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;

// Global constants to represent rock,
// paper, or scissors.

const  int rock = 1;
const int paper = 2;
const int scissors = 3;

int getComputerChoice(int);
int getUserChoice(char);
void determineWinner(int, int);

int main()
{
    int compChoice;
    char uChoice;

    getComputerChoice(compChoice);
    getUserChoice(uChoice);
    if (uChoice == 'r' || uChoice == 'p' || uChoice == 's') {
        determineWinner(compChoice, uChoice);
        getComputerChoice(compChoice);
        getUserChoice(uChoice);
    }

    return 0;
}

// ********************************************************
// The getComputerChoice function returns the computer's  *
// game choice. It returns 1 for rock (via the ROCK       *
// constant), or 2 for paper (via the PAPER constant),    *
// or 3 for scissors (via the SCISSORS constant).         *
// ********************************************************

int getComputerChoice(int compChoice) {


    // Get the system time so we can use it
    // to seed the random number generator.
    unsigned seed = time(0);


    // Use the seed value to seed the random
    // number generator.
    srand(seed);

    // Generate a random number in the range of 1-3.
    compChoice = (1 + rand() % 3);

    return compChoice;
}


// ********************************************************
// The getUserChoice function displays a menu allowing    *
// the user to select rock, paper, or scissors. The       *
// function then returns 1 for rock (via the ROCK         *
// constant), or 2 for paper (via the PAPER constant),    *
// or 3 for scissors (via the SCISSORS constant).         *
// ********************************************************

int getUserChoice(char uChoice) {


    cout << "Welcome to rock, paper, scissors. Choose 'r' for rock, 'p' for paper, or 's' for scissors.\n";

    if (uChoice == 'r' || uChoice == 'p' || uChoice == 's')
        cin >> uChoice;
    else
        cout << "This is not a valid choice.\n";


    return uChoice;

}

// ********************************************************
// The determineWinner function accepts the user's game   *
// choice and the computer's game choice as arguments and *
// displays a message indicating the winner.              *
// ********************************************************

void determineWinner(int compChoice, char uChoice) {
    // Display the choices.
    switch (1) {
    case 'r':
        if (compChoice == 1)
            cout << "Both of you picked rock, it's a tie./n";
        else if (compChoice == 2)
            cout << "You lost, you picked rock and the computer picked paper.\n";
        else
            cout << "You won! You picked rock and the computer picked scissors.\n";
        break;
    case 'p':
        if (compChoice == 1)
            cout << "You won! You picked paper and the computer picked rock.\n";
        else if (compChoice == 2)
            cout << "Both of you picked paper, it's a tie./n";
        else
            cout << "You lost, you picked paper and the computer picked scissors.\n";
        break;
    case 's':
        if (compChoice == 1)
            cout << "You lost, you picked scissors and the computer picked rock.\n";
        else if (compChoice == 2)
            cout << "You won! You picked scissors and the computer picked paper.\n";
        else
            cout << "Both of you picked scissors, it's a tie./n";
        break;
    default:
        cout << "Sorry, something's wrong. Try again.";
    }
}

【问题讨论】:

  • 嗯,您的代码中没有 name。另外,请创建minimal reproducible example,您可能想阅读good book
  • 这个问题与函数原型无关。我认为您需要打开书本并阅读有关局部变量、参数和可变范围的信息。特别是,赋予事物相同的名称并不会使它们成为相同的事物(问 John Smith)。
  • 还有其他问题:您正在阅读uChoice 测试其价值之后。 switch (1) 不是很有用,因为 1 不能是 'r''p''s' 中的任何一个。您并不总是使用指定的常量进行选择。而且你应该只设置一次随机种子(main 早期是一个好地方)。

标签: c++ compiler-errors function-prototypes


【解决方案1】:

编写的代码永远不会工作。在main 中,您将compChoice 传递给getComputerChoice 并将uChoice 传递给getUserChoice,而无需初始化它们中的任何一个。代码应该更像:

int compChoice = getComputerChoice ();
int uChoice = getUserChoice ();

并相应地修改函数原型。 getUserChoice 的实现也是不正确的,但我会留给您对它进行排序(在测试之前读入变量!)。并查看 molbdnilo 关于 switch (1) 的第二条评论,必须解决这个问题。

其他评论员是对的,您确实需要做一些背景阅读,但话又说回来,没有什么可以代替动手做的。不要放弃,学习如何使用一个好的调试器来单步调试你的代码,这会让你更深入地了解它实际在做什么(Visual Studio 调试器特别好)。

【讨论】:

    猜你喜欢
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    相关资源
    最近更新 更多