Well I've been doing a project to create a user friendly and fully functional lotto system. I'm at a step where I want a bot(the computer) to enter the rest of the numbers. Some reason When the bot gets activated the numbers generated are rejected, run the program and see what I mean.
Code:
/* Version 5.3
Version 5.3
Version 5.3
*/
#include <stdio.h>
#include <stdlib.h>
void lottoMessage(void); // function for opening message
int searchLottoNums(int num); // function for checking is number is already taken
int lottoWinner(void); // function for finding the winner
int bots(int i); // function for bots
int replay(void); // Global variable
int lottoNums[150]; // Global variable
int i; // Global variable
int count; // Global variable
int count1; // Global variable
int num; // Global variable
int x = 0; // Global variable
int main(void)
{
int start;
int winNum;
printf("Enter \"1\" to start lotto Game!");
if (scanf("%d", &start) == 1 && start < 2 && start > 0)
{
lottoMessage(); // line 69. Just a message
fflush(stdin);
for(i = 1; i <= 150;) // loop to start accepting numbers
{
printf("\nEnter your lucky number: ");
scanf("%d", &num);
if (num == 999)
{
printf("Bots will run here\n");
bots(i);
}
else if(num >= 1 && num <= 150) // checking for a valid number entry
{
searchLottoNums(num); // line 76. to checking for available numbers
if (searchLottoNums(num) == 0) // reserving number in array
{
printf("The number entered was %d\n", num);
lottoNums[x] = num;
x++;
i++;
printf("accepted %d", x);
fflush(stdin);
}
else
{
printf("The number entered is already taken!\n");
replay();
printf("%d", i);
fflush(stdin);
}
}
else
{
fflush(stdin);
printf("Didn't enter a Valid number!!!\n");
replay();
printf("%d", i);
}
}
lottoWinner(); // line 102. Finding the winner
}
else
{
printf("Lotto system wasn't started!!!!");
}
return 0;
}
void lottoMessage(void)
{
printf("\nThe lotto jackpot is $1,000,000!!!!\n");
printf("Enter your lucky number today for a chance to win BIG!!!!!\n");
printf("Remeber there will ONLY be ONE winner!\n\n");
}
int searchLottoNums(int num)
{
for(count = 0; count < 150; count++)
{
if(lottoNums[count] == num)
{
return 1;
break;
}
}
return 0;
}
int replay(void)
{
i++;
if(i == 0)
{
return 0;
}
else
{
i = i - 1;
}
}
int lottoWinner(void)
{
int winNum;
srand(time(NULL));
winNum = (rand() % 149) + 1;
for(count = 0; count < 150; count++)
{
if(winNum == lottoNums[count])
{
printf("The lotto winning lotto number is......\n");
printf("%d", winNum);
}
else
{
return 0;
}
}
}
int bots(int i)
{
for(count1 = i; count1 <=150;)
{
srand(time(NULL));
num = (rand() % 149) + 1;
searchLottoNums(num);
if (searchLottoNums(num) == 0) // reserving number in array
{
printf("The number entered was %d\n", num);
lottoNums[x] = num;
x++;
i++;
printf("accepted %d\n", x);
fflush(stdin);
}
else
{
printf("The number entered is already taken!\n");
replay();
printf("%d", i);
fflush(stdin);
}
fflush(stdin);
count1++;
i++;
}
return 0;
}
Can any tell me why this happens?