GOOD POINT, never noticed that.
so how about this
#include <iostream>
using namespace std;
bool check (unsigned int checkvariable[], int a);
int main()
{
srand(time(NULL));
unsigned int lotteryBall[9];
for(int i = 0; i <=8; ++i)
{
do
{
lotteryBall[i] = rand() % 10 + 1;
}
while (check(lotteryBall, i));
if(i == 8)
cout << "*" << lotteryBall[i] << "*";
else
cout << lotteryBall[i] << " ";
}
cin.get();
return 0;
}
bool check (unsigned int checkvariable[], int a)
{
for(int b=0;b<=a;b++)
{
if (a==b)
{
return false;
break;
}
if(checkvariable[b]==checkvariable[a])
{
return true;
break;
}
}
}
ONE QUESTION: I THOUGHT I READ THAT IF YOU WANTED TO USE THE SRAND() FUNCTION YOU MUST INCLUDE THE DIRECTIVE: ctime. In this case I did not and it still works??
Yeah, that program works well. srand is from cstdlib, not ctime. NULL is in ctime and cstdlib. I don't know why it works without cstdlib, but it does. Still, probably best to #include it anyway to be safe. If you don't #include iostream and add
using namespace std , you seem to run into problems with this program regarding srand. Not sure why.
Reputation Points: 2614
Solved Threads: 687
Posting Expert
Offline 5,372 posts
since Jan 2008