Code doesnt store / print rnd values properly

Hello,

while learning C++, I decided to write a brief lottery programm. The programm takes six digits of data in from the user, then generates six random values (between 1 and 49) and then compares them. eventually it will determine weather the user guessed the random numbers right or not (very very unlikely to guess them right)
I wrote the program on Xubuntu with Code::Blocks and compiled and linked them with g++.
The problem is, that the program prints the generated numbers in the end not correct. if you didnt win the lottery, it should be printing the correct numbers onto the screen, which it doesnt.
Code:
Main.cpp
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
#include <iostream>

using namespace std;

//declaring variables, declared globally because they are used in two files (functions)
//A stands for the first number that is drawn / input and F stands for the last one
//g_unX stores the number that the user inputs
unsigned int g_unA = 0, g_unB = 0, g_unC = 0, g_unD = 0, g_unE = 0, g_unF = 0;

//g_unLX stores the random number that the PC generates as "true value to win the lotto"
unsigned int g_unLA = 0, g_unLB = 0, g_unLC = 0, g_unLD = 0, g_unLE = 0, g_unLF = 0;

//declaring the function prototype for later use in main() even though the function is stored in calc.cpp
bool bLottoWinner (unsigned int g_unA, unsigned int g_unB, unsigned int g_unC, unsigned int g_unD, unsigned int g_unE,
                    unsigned int g_unF);

int main()
{

    //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";
    cin >> g_unA;
    cin >> g_unB;
    cin >> g_unC;
    cin >> g_unD;
    cin >> g_unE;
    cin >> g_unF;

    //determines if the user has won the match, and print the appropriate message to the screen
    //uses the returned value of the bLottoWinner function to determine which message to print
    if(bLottoWinner(g_unA, g_unB, g_unC, g_unD, g_unE, g_unF))
        cout << "\nCongratulation, you have won!!" << endl;

    else
    {
        cout << "Sorry, you have lost... Why don't you try again? \nThe correct numbers were:\n" <<
        g_unLA << " " << g_unLB << " " << g_unLC << " " << g_unLD << " " << g_unLE << " " << g_unLF << endl;
    }

    return 0;
}


calc.cpp:
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
#include <ctime>
#include <stdlib.h>

//defining the function that has been declared in main.cpp
//this function generates the random numbers for Lotto and then compares them to the users input
//to determine weather he has won. if he won, return true; if he did not return false;
bool bLottoWinner (unsigned int g_unA, unsigned int g_unB, unsigned int g_unC, unsigned int g_unD, unsigned int g_unE,
                    unsigned int g_unF)

{
    //seeding the random number generator
    srand(time_t());

    //defining the max value that the random Lotto number is allowed to have
    const unsigned int unMaxLottoValue = 49;

    //defining i, i exists just for testing and debugging purposes
    int i = 0;

    //applying the (random number % unMaxLottoValue) to its corresponding int value
    unsigned int g_unLA = (rand() % unMaxLottoValue);

    //testing weather the random number was 0, if it was add 1, if it was not increase i by 1
    g_unLA == 0 ? g_unLA + 1 : i++;
    unsigned int g_unLB = (rand() % unMaxLottoValue);
    g_unLB == 0 ? g_unLB + 1 : i++;
    unsigned int g_unLC = (rand() % unMaxLottoValue);
    g_unLC == 0 ? g_unLC + 1 : i++;
    unsigned int g_unLD = (rand() % unMaxLottoValue);
    g_unLD == 0 ? g_unLD + 1 : i++;
    unsigned int g_unLE = (rand() % unMaxLottoValue);
    g_unLE == 0 ? g_unLE + 1 : i++;
    unsigned int g_unLF = (rand() % unMaxLottoValue);
    g_unLF == 0 ? g_unLF + 1 : i++;

    //if the numbers, which the user entered are equal to those that the computer generated, return true, otherwise return false
    if(g_unA == g_unLA && g_unB == g_unLB && g_unC == g_unLC && g_unD == g_unLD && g_unE == g_unLE && g_unF == g_unLF/* && i == 6*/)
        return true;

    else
        return false;
}


moreover, when i print the "i" that i use in calc.cpp, it is always six, meaning that there are random numbers being stored in the g_unLX variables, because they are defined as 0 upfront.

thank you very much for helping me

replax
Thanks for using code tags!

What does the program output look like?

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
}
Topic archived. No new replies allowed.