Thread: Lotto SystemV5

  1. #1
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134

    Lotto SystemV5

    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?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    For starters, srand should only be called once, ever; certainly not 150 times in a loop where you'll reset the random number generator to the same starting number each time and generate all the same numbers.

    Past that, thinking about what globals called "i" and "x" could possibly mean (why is it global? why is it called i? the world will never know) is too silly to contemplate actually doing. (Especially when you have a global i, and you have a local i too! Quick: which functions use the global i and which functions don't?! Could it be a bug!?

  3. #3
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134
    So what is a better way of generating numbers?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by BIGDENIRO View Post
    So what is a better way of generating numbers?
    Quote Originally Posted by tabstop
    For starters, srand should only be called once, ever
    -- ideally at the start of your main function and then not ever again.

  5. #5
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134
    The i Global variable is for the for loop in the main and the x is for the reserved number index. Bot I have another problem, the bot doesn't end the for loop in the main

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by BIGDENIRO View Post
    The i Global variable is for the for loop in the main and the x is for the reserved number index. Bot I have another problem, the bot doesn't end the for loop in the main
    Well, how can it? Your for loop is controlled by an i (one of your i's, you have two). How are you going to change that value of i?

  7. #7
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134
    Well thanks for your tips, I'm going to rewrite the code with same structure but with more variable control.

  8. #8
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134
    PS I don't see whats wrong with naming my counter variable i

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by BIGDENIRO View Post
    PS I don't see whats wrong with naming my counter variable i
    It would be perfectly wonderful to call a counter variable i. But no counter variable is ever global, because you're only using it in a loop. (NOTE: This means that if you intend your i to be a counter variable, it shouldn't be global.)

  10. #10
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134
    OK got it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lotto program
    By MrQ in forum C Programming
    Replies: 3
    Last Post: 06-24-2013, 12:35 AM
  2. lotto program in c
    By vyshant in forum C Programming
    Replies: 7
    Last Post: 11-07-2011, 12:19 PM
  3. Lotto problem
    By Roaring_Tiger in forum C Programming
    Replies: 11
    Last Post: 03-13-2003, 10:17 PM
  4. Lotto game in C
    By fun2sas in forum C Programming
    Replies: 2
    Last Post: 03-02-2003, 07:19 PM
  5. Combinations, lotto, 6/49
    By Robert in forum C++ Programming
    Replies: 8
    Last Post: 12-06-2002, 07:27 PM