Unique number not unique

I'm creating a new topic picking up where I left the old one cause the topic title isnt accurate, and to keep track of progress in solving the problems I have with the program.

This is what I have now:
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
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <stdlib.h>
#include <time.h>

const int MAX =10;

class Baz
{
    public:
        int number;
        std::string name;
};

class Foobar
{
    public:
        Baz bar[MAX];

        Foobar()
        {
            int random;
            std::cout << "\nInteger random created at adress " << &random;

            for (int j=0; j < MAX; j++)
            {
                 //GIVE VALUE OF 'random' TO 'bar[].number'
                 random = rand()%1000;
                 bar[j].number = random;

                 std::cout << "\nFoobar constructor generated random number " << random << " at adress " << &bar[j].number;
            }
        }//END constructor Foobar()


        void newBaz()
        {
            int random;
            int i;
            bool isPresent;
            std::cout << "\nInteger random created at adress " << &random;

            do
            {
             // random = rand()%1000-9999;

                isPresent = false;
                std::cout << "\nBool created at adress " << &isPresent;

                //CHECK IF VALUE OF 'random' IS PRESENT IN 'bar[].number'
                std::cout << "\nCheck if random generated number is unique";
                for(i=0; i < MAX; i++)
                {
                    //IF VALUE IS PRESENT
                    if(bar[i].number == random)
                    {
                        std::cout << "\nGenerated number for " << bar[i].number << " is NOT unique";
                        //GIVE 'random' NEW VALUE
                        random = rand()%1000;
                        std::cout << "\nGenerated new number";

                        isPresent = true;
                        break;
                    }//END if

                }//END for

            //CHECK AGAIN?
            if(i==MAX)
                isPresent = false;

            }while(isPresent);
            std::cout << "\nAll numbers are unique";

//          bar[0].number = random;

            std::cin >> bar[0].name;
        }//END newBaz()
};

int main()
{
    srand(time(0));
    Foobar qux;
    std::cout << "\nFoobar object created at adress " << &qux;

    qux.newBaz();

    return 0;
}
Integer random created at adress 0x22ff18
Foobar constructor generated random number 379 at adress 0x22fec8
Foobar constructor generated random number 151 at adress 0x22fed0
Foobar constructor generated random number 388 at adress 0x22fed8
Foobar constructor generated random number 363 at adress 0x22fee0
Foobar constructor generated random number 197 at adress 0x22fee8
Foobar constructor generated random number 493 at adress 0x22fef0
Foobar constructor generated random number 978 at adress 0x22fef8
Foobar constructor generated random number 98 at adress 0x22ff00
Foobar constructor generated random number 705 at adress 0x22ff08
Foobar constructor generated random number 437 at adress 0x22ff10
Foobar object created at adress 0x22fec8
Integer random created at adress 0x22ff18
Bool created at adress 0x22ff1f
Check if random generated number is unique
Generated number for 437 is NOT unique
Generated new number
Bool created at adress 0x22ff1f
Check if random generated number is unique
All numbers are unique
Process returned 0


The question is why the program generates a unique number, and then tells its NOT unique and creates a new one? It sounds like an unnecessary step...
You have forgot to give random a value before using it on line 54.
`random' is used without being initialized.
Topic archived. No new replies allowed.