It looks like you're misunderstanding variable scope.
You have a global variable
g_unLA
which is defined in main.cpp.
Notice that you're declaring g_unLA
again in calc.cpp (inside the bLottoWinner function).
Even though these variables have the same name, they are
two different variables because they exist in different scopes. Therefore when you change g_unLA in your bLottoWinner function, the global g_unLA variable remains unchanged.
That's why it prints 0 when you print the variable from main.
The solutions here:
1) pass g_unLA (and the other vars) by pointer/reference to the bLottoWinner function so it can change them
2) make bLottoWinner change the global variables directly instead of making new variables.
I recommend option 1. Globals are a very bad habit to get into. You really shoud avoid using them.
Furthermore... did you learn about arrays yet? It would make much more sense if you had arrays for the lotto numbers and the user input. By having seperate variable names for each number, you force yourself to repeat the same code 6 times. Arrays let you compact stuff into loops.
Also, your lotto is biased. The range is [1..48] and 1 is twice as likely to come up as any other number. If you want the lotto to be [1..49] then just add 1 unconditionally to the randomly generated number:
|
x = (rand() % unMaxLottoValue) + 1; // [1..49]
|
Here are [some of] the changes I recommend making:
1 2 3 4 5 6 7 8 9 10
|
// calc.h
// make a constant for the number of numbers in the lotto. So it can be easily changed, and you don't have
// to remember that 6 is the magic number
static const int LOTTOCOUNT = 6;
// if function bodies exist in seperate cpp files, put their prototypes in a header
// also, this function now takes 2 arrays (by pointer) as parameters, rather than 6 individual numbers
bool bLottoWinner(const unsigned int userguess[LOTTOCOUNT], unsigned int actuallotto[LOTTOCOUNT]);
|
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
|
// main.cpp
...
#include "calc.h" // include to get the constants / prototype
// don't have any global vars
int main()
{
// instead, make the vars local to main
unsigned int userguess[LOTTOCOUNT];
unsigned int actuallotto[LOTTOCOUNT];
//asks the used to input his guessed values and then stores them in the g_unX int
cout << "Enter your six Lotto numbers between 1 and 49, press Enter after each one.\nEnter below:\n";
// now that we are using arrays, we can compact duplicate code into loops:
for(int i = 0; i < LOTTOCOUNT; ++i)
cin >> userguess[i];
// pass the arrays to the other bLottoWinner function
if(bLottoWinner(userguss,actuallotto))
cout << "\nCongratulation, you have won!!" << endl;
else
{
cout << "Sorry, you have lost... Why don't you try again? \nThe correct numbers were:\n" <<
// you can print the actual numbers here with a loop as well
}
return 0;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
// calc.cpp
...
#include "calc.h"
...
bool bLottoWinner(const unsigned int userguess[LOTTOCOUNT], unsigned int actuallotto[LOTTOCOUNT])
{
// no need to create new variables here. You can use userguess and actuallotto directly
// changes to actuallotto will change the same actuallotto in main()
// you can also use loops to get rid of duplicate code
}
|