### 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:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889`` ``````#include #include #include 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.