Below is my code for a program that is suppose to generate and display six unique random numbers (between 1 and 54). The issue seems to be in my check for duplicates. I know what I am doing wrong but can't seem to find a way to fix it. I think it is getting stuck in an endless loop because the way I have written it looks like it checks the first value against itself which will of course look like a duplicate everytime. I am relatively new to programming, but catch on quickly....just a point in the right direction would be of immense help. Thanks.
Code:#include<iostream> #include<ctime> using namespace std; //function prototype int getRandNumb(); int main () { srand((unsigned)time(NULL)); //declare array int randomNum[6]; //declare variable int checkNum = 0; for (int x = 0; x < 6; x += 1) { randomNum[x] = getRandNumb(); if (randomNum[x] == randomNum[0] || randomNum[1] || randomNum[2] || randomNum[3] ||randomNum[4] || randomNum[5]) x -= 1; else cout << randomNum[x] << endl; } //end for cin.get(); return 0; } //end of main function //*****function definition(s)***** int getRandNumb() { return rand()%54 - 1 + 1; } //end of getRandNumb function


Reply With Quote
Bookmarks