Uinitialized Local variable in Visual Studios ?

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.

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
 *Name: Rose Emily Campos
 *Lab:PRO
 *Description: 
 **/
#include <iostream>
#include <string>
#include <cstdlib> 
#include <ctime> 
using namespace std;

//Function Prototypes
int randNumber(int); //Get random number
void eightBall(string[]);//Get message

//Universal Variable
const int 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
Line 19? That warning
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.
Last edited on
Oh! It was because I was re-initializing it and it was already a global variable?
Thanks so much @highwayman!
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.
Topic archived. No new replies allowed.