0

I am a high school student and for one of my final projects my assignment includes function prototypes. I will include the code below but the error that always shows up is "uninitialized local variable "name" used". I define that variable in a separate function and return it, but it does not go back to the int main. I'm sure it's something obvious but if anyone could help me I would appreciate it so much. Thanks

    // 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.";
    }
}
  • 3
    Hmm there is no `name` in your code. Also, please create a [mcve] and you might want to read a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Rakete1111 May 08 '18 at 14:31
  • 2
    This problem has nothing to do with function prototypes. I think you need to open your book and read about local variables, parameters, and vaiable scope. In particular, giving things the same name does not make them the same thing (ask John Smith). – molbdnilo May 08 '18 at 14:36
  • 2
    There are other issues: you're reading `uChoice` *after* testing its value. `switch (1)` isn't very useful, since `1` can't be either of `'r'`, `'p'`, or `'s'`. You're not always using the designated constants for the choices. And you should only set the random seed once (early in `main` is a good spot). – molbdnilo May 08 '18 at 14:45

1 Answers1

0

The code as written is never going to work. In main, you are passing compChoice to getComputerChoice and uChoice to getUserChoice without initialising either of them. The code should be more like:

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

and modify the function prototypes accordingly. The implementation of getUserChoice is also not right but I'll leave it to you to sort that one out (read in the variable before you test it!). And see molbdnilo's second comment about switch (1), gotta fix that.

The other commentators are right, you do need to do a bit of background reading, but then again, there's no substitute for a bit of hands-on work. Don't give up, and learn how to use a good debugger to step through your code which will give you a lot more insight into what it's actually doing (the Visual Studio debugger is particularly good).

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48