The program is supposed to work as a basic Magic 8 ball. I built it in a sandbox and made sure it worked. The only feedback there was a warning but not an error. However, when I placed the program on Visual Studios, it gave me an error declaring "uninitialized local variable 'randNum' used' on line 64. How do I fix this? Because the code was able to run in the sandbox but not VS.
/**
*Name: Rose Emily Campos
*Lab:PRO
*Description:
**/
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
usingnamespace std;
//Function Prototypes
int randNumber(int); //Get random number
void eightBall(string[]);//Get message
//Universal Variable
constint ARRAY_SIZE = 8;
string ballResponse[ARRAY_SIZE];
int randNum;
//Start of Main
int main()
{
// Variables
bool quit_game = false;
string question, name;
cout << "Please enter your name to start: ";
getline(cin,name);
cout << "\nWelcome to the Magic 8-ball game, " << name << "!" << endl;
cout << "Please enter a yes or no question for me to answer." << endl;
cout << "If at any time you want quit the game, ";
cout << "type -exit- and the program will end. \n\n";
//Repeated Program
while (!quit_game)
{
cout << "Ask a question and I will tell you the future!" << endl;
getline(cin,question);
/**
* Check for exit. If input for question
*is "exit" then bool becomes true,
*terminating the loop.
**/
if (question.compare("exit") == 0)
quit_game=true;
else
eightBall(ballResponse);
cout << "\n\n";
}
}//End of Main
//Start of randNumber
int randNumber(int randNum){
srand(time(0));
randNum = rand() % 9;
return randNum;
}// End of randNumber
//Start of eightBall
void eightBall(string ballResponse[ARRAY_SIZE]){
int randNum = randNumber(randNum);
string printString;
//Responses stores
ballResponse[0] = "As I see it, yes.";
ballResponse[1] = "Don't count on it.";
ballResponse[2] = "You may rely on it.";
ballResponse[3] = "Outlook good.";
ballResponse[4] = "My sources say no.";
ballResponse[5] = "It is decidedly so.";
ballResponse[6] = "My reply is no.";
ballResponse[7] = "Outlook not so good.";
//Get response & print
printString = ballResponse[randNum%ARRAY_SIZE];
cout << printString;
}//End of eightBall
On line 64 you're attempting to pass in an uninitialized local variable (randNum), the same variable you're trying to initialize as the result of the function call. This doesn't really make sense.
Change your randomNumber function.
1. Change the prototype on line 13 to: int randNumber(); //Get random number
2. Change the function definition to
1 2 3 4
int randNumber(){
int randNum = rand() % 9;
return randNum;
}
3. Notice: I removed the call to srand(time(0)). Call this once, at the start of main instead.
4. change line 64 to int randNum = randNumber();
And as highwayman alluded to, remove the global variable on line 19. You don't need this, and it's error-prone.
and there you have just one of the many problems caused by global variables and why people keep telling you to not use them. If you had initialized it you would have had a silent bug to fix.