8
\$\begingroup\$

I have developed a Lotto Game in C for my Assignment, it allows a user to enter one of the 6 menu options.

From the menu the user would be able to view the numbers they have chosen, another option would sort the numbers in ascending order, in this case I used bubble sort.

What it does in the 1st menu is read the 6 numbers that the user has entered from the keyboard. The number must range from 1-42 and if the user enters any number out of bounds it will give an error message. It also gives an error message if the user enters a number that they has entered already.

Another option will compare the users numbers with the winning numbers and see if they match.

It will also display how many times a number has been entered, for example if the user entered 5 twice from 4 games it will display Number 5 has been selected 2 times. without exiting the program. This can be selected from the menu too.

Last but not least, the last option from the menu exits the program.

I added key features such as displaying the menu after finishing with the current option from the menu and options 2,3,4 and 5 only to be selected if option 1 has been selected from the menu.

Please take a look and let me know how this code could be improved to make this a better Lotto Game.

Here is the code:

#include <stdio.h>
#include <stdlib.h>
#define SIZE 6           // Size of the array as only 6 numbers are entered.
#define MAXOUT 43       // For Frequency use when displaying the numbers.
#define ZERO 0         // Replacing 0 with Zero.
#define ARRAYSIZE 500 // Storing the frequency, 500 should be enough.

/*Prototypes*/

//For Option 1
//
void menu_1(int*, int*);

//For Option 2
//
void menu_2(int*);

//For Option 3
//
void menu_3(int*);

//For Option 4
//
void menu_4(int*, int*); 

//For Option 5
//
void menu_5(int*, int*);

//For Option 6
//
void menu_6();

//Ninja fix for entering the number twice.
//
int error_dubhandle(int *user_lottonumber);

//Out of bound error fix
//
void error_handle1();

//Main
//
int main(void)
{
    //Main Variables.
    //
    int start; // Used to select from the menu.
    int loop; // To display the menu over and over.

    loop = ZERO; // To display the menu over and over in the do while loop.

    //User error checking select 1 before selecting other options.
    //
    int selectOnefirst = 0;

    //Where the user numbers are stored.
    //
    int user_lottonumber[SIZE];

    //The winning numbers.
    //
    int winning_numbers[SIZE] = {1,3,5,7,9,11};

    //Frequency array storage
    //
    int frequencyChecker[ARRAYSIZE] = {ZERO};

    //The loop for the main to be displayed over and over
    //Until the program ends.
    //
    do
    {
        //Printing out the menu. With a nice boarder around it.
        //
        printf("\n--------------------------------------------Gabriel Grimberg---------------------------------------------------");
        printf("\n***************************************************************************************************************");
        printf("\n* 1. - Enter your numbers in to play.                                                                         *");                                                                                       
        printf("\n* 2. - Display the numbers you enters.                                                                        *");
        printf("\n* 3. - Sort your numbers in an increasing order.                                                              *");
        printf("\n*      (This is mandatory, it will sort the numbers you entered in an increasing order.)                      *");
        printf("\n* 4. - See if you won the lotto!                                                                              *");
        printf("\n* 5. - Display the frequency of the numbers. (Record of times a number has been selected.)                    *");
        printf("\n* 6. - Exit the Lotto Game.                                                                                   *");    
        printf("\n***************************************************************************************************************\n\n");

        //Letting the user know to press 1 before pressing 2-5.
        //
        printf("You must complete option 1 from the menu before moving on.");

        printf("\n\nPlease select from the menu: ");

        //Entering from the menu.
        //
        scanf("%d", &start);

        fpurge(stdin); //Clears the buffer of given stream.

        //Making sure the user can only select the options from 1-6 from the menu.
        //
        if(start > 6)
        {
            error_handle1();
        }

        //Calling the function to allow the user to enter the numbers.
        //
        if(start == 1)
        {
            menu_1(user_lottonumber,frequencyChecker);
            selectOnefirst++;
        }

        //Calling the function to show the user entered numbers.
        //
        if(start == 2 && selectOnefirst == 1)
        {           
            menu_2(user_lottonumber);
        }

        //Calling the function to sort the numbers in an increasing order.
        //
        if(start == 3 && selectOnefirst == 1)
        {
            menu_3(user_lottonumber);
        }

        //Calling the function to see if the user won the lotto.
        //
        if(start == 4 && selectOnefirst == 1)
        {
            menu_4(user_lottonumber,winning_numbers);
        }

        //Calling the function to see the frequency of numbers
        //
        if(start == 5 && selectOnefirst == 1)
        {
            menu_5(user_lottonumber, frequencyChecker);
            selectOnefirst--;
        }

        //Calling the function to exit the program.
        //
        if(start == 6)
        {
            menu_6();
        }

    }
    while (loop == ZERO); //end loop

}//End Int Main()

/*Implementing Functions*/

//Getting the users numbers
//
void menu_1(int* user_lottonumber, int* frequencyChecker)
{
    //Declaring the Variables.
    //
    int i;
    int number_checker;
    number_checker = 1;

    //Resetting the Array to 0.
    //
    for(i = ZERO; i < SIZE; i++)
    {
        *(user_lottonumber + i) = ZERO;
    }

    //Displaying to the user the information.
    //
    printf("Welcome to the Lotto Game! Time to pick your numbers.\n");

    printf("Note: You must pick from 1-42\n\n"); //Telling the user to pick from 1-42.

    for(i = ZERO; i < SIZE; i++) 
    {
        do
        { 
            number_checker = 1;

            //Telling the user to enter in their 6 numbers.
            //
            printf("\nPlease enter your %d numbers in: ",SIZE);
            scanf("%d", user_lottonumber + i);

            fpurge(stdin); //Clears the buffer of given stream.

            //Error checking.
            //
            if(error_dubhandle(user_lottonumber) || (!(*(user_lottonumber + i) > ZERO && *(user_lottonumber + i) < 43) )) 
            {
                number_checker = ZERO;
                printf("\nThe number you have selected is either already selected or invalid.\n");
                printf("\nPlease select a number between 1-42 also don't select the same number again.\n");
            }

            //Calculating the Frequency.
            //
            if(number_checker == 1) 
            {
                //Checking the numbers entered on against the numbers on the frequencyChecker.
                //
                *(frequencyChecker + *(user_lottonumber + i)) = *(frequencyChecker + *(user_lottonumber + i)) + 1;
            }

        }//End do

        while(!number_checker); //While not 0 run the code.

    }//End for

}//End menu_1()

//Displaying the user entered numbers.
//
void menu_2(int *user_lottonumber)
{
    //Declaring the Variables.
    //
    int i;

    printf("Here are the numbers that you have selected: \n");

    for(i = ZERO; i < SIZE; i++)
    {
        printf("%d ",*(user_lottonumber+i));
    }

}//End main2()

//Organising the User Lotto numbers.
//
void menu_3(int *user_lottonumber)
{
    //Declaring the Variables.
    //
    int n;
    int m;
    int place;

    //Sorting using Bubble Sort.
    //
    for(n = ZERO; n < SIZE; n++)
    {
        for(m = n; m < SIZE; m++)
        {
            if(*(user_lottonumber+n) > *(user_lottonumber+m)) //Sorting in an ascending order. 
            {
                place = *(user_lottonumber+n);
                *(user_lottonumber+n) = *(user_lottonumber+m);
                *(user_lottonumber+m) = place;

            }//End if.

        }//End for.

    }//End for.

    //Displaying the ordered numbers.
    //
    printf("Here are your numbers ordered: \n");

    for(n = ZERO; n < SIZE; n++)
    {
        printf("%d ", *(user_lottonumber+n) );
    }

}//End main3()

//Seeing if the user won
//
void menu_4(int *user_lottonumber, int *winning_numbers)
{
    //Declaring the Variables.
    //
    int i;
    int c;
    int compareNum;
    int bonusNum;
    int bonusCount;
    bonusNum   = 42; //Setting the bonus number.
    bonusCount = ZERO;
    compareNum = ZERO;

    //The counter that checks if the user matches the winning numbers.
    //
    for(i = ZERO; i < SIZE; i++)
    {
        for(c = ZERO; c < SIZE; c++)
        {
            if(*(user_lottonumber+i) == *(winning_numbers+c))
            {
                compareNum++; //Incrementing.
            }
        }
    }

    //The counter that checks if the user has the bonus number
    //
    for(i = ZERO; i < SIZE; i++)
    {
        if(*(user_lottonumber) == bonusNum)
        {
            bonusCount++; //Incrementing.
        }
    }

    //If 6 numbers match the user wins this.
    //
    if(compareNum == 6)
    {
        printf("Congratulations, You have won the Jackpot!");
    }

    //If 5 numbers and the bonus matches, the user wins this.
    //
    if(compareNum == 5 && bonusCount == 1)
    {
        printf("Congratulations, You have won a Car!");
    }

    //If 5 numbers match the user wins this.
    //
    if(compareNum == 5)
    {
        printf("Congratulations, You have won a Holiday!");
    }

    //If 4 numbers and the bonus matches, the user wins this.
    //
    if(compareNum == 4 && bonusCount == 1)
    {
        printf("Congratulations, You have won a Weekend Away!");
    }

    //If 4 numbers match the user wins this.
    //
    if(compareNum == 4)
    {
        printf("Congratulations, You have won a Night Out!");
    }

    //If 3 numbers and the bonus matches,c the user wins this.
    //
    if(compareNum == 3 && bonusCount == 1)
    {
        printf("Congratulations, You have won a Cinema Ticket!");
    }

    //If the none of the numbers the user entered match, the user doesn't win anything.
    //
    if(compareNum == ZERO || compareNum == 1 || compareNum == 2 || compareNum == 3)
    {
        printf("Hard luck, you have not won anything.");
    }

}//End menu_4()

//Frequency of numbers.
//
void menu_5(int *user_lottonumber, int *frequencyChecker)
{
    //Declaring the Variables.
    //
    int i;
    int j;
    int adder;

    //Prints the frequency of each element.
    //
    for(i = ZERO; i < MAXOUT; i++)  
    {  
        if(*(frequencyChecker+i) != ZERO)  
        {  
            printf("Number %d has been selected %d times.\n", i, *(frequencyChecker+i) );  
        }  
    }

}//End menu_5()

//Exiting the program
//
void menu_6()
{
    //Displaying an exit message.
    //
    printf("\nYou have chosen to exit the program. Thanks for playing the Lotto Game!\n");

    //Terminating the code.
    //
    exit(ZERO);

}//End menu_6()

//Error Checking making sure the user doesn't enter the same number in again.
//
int error_dubhandle(int *user_lottonumber)
{
    //Declaring the Variables.
    //
    int adding;
    int atk;

    for (adding = ZERO; adding < SIZE; adding++) 
    {
        for (atk = adding + 1; atk < SIZE; atk++) 
        {
            //Checks if the array has any duplicates.
            //
            if (*(user_lottonumber + adding) == *(user_lottonumber + atk) && *(user_lottonumber + atk) != ZERO) 
            {
                return(1); //If there is a duplicate then return 1.

            }//End if.

        }//End second for.

    }//End first for.

    return(ZERO); //If there is no duplicate return 0.

}//End error_dubhandle();

//Out of bound error fixer
//
void error_handle1()
{
    //Showing the user the problem, in this case if the user doesn't select
    //between 1-6 from the menu then this will show up.
    //
    printf("Invalid Option, please select again and read the instructions carefully!");
    printf("\n-->You are now going to be shown the menu again, please select appropriately.<--\n");

}//End error_handle1()
\$\endgroup\$
10
\$\begingroup\$

Comments

Comments should tell the reader why you're doing something, not what you're doing. Comments like this are redundant:

//For Option 6
//
void menu_6();

Method Naming

Consider giving your functions more meaningful names that describe what they do. Using the example above, menu_6. Unless you already have the menu in your head the name is meaningless. A better name might be menu_exit_game.

Magic Numbers Vs Defines

Replacing magic numbers with meaningful constants is a good thing, however simply replacing a magic number with it's word value adds little value. Consider:

#define ZERO 0

What should ZERO be used for? You're using it as a direct replacement for 0 in your code, but not always:

int selectOnefirst = 0;

Another example is SIZE, SIZE of what? NUM_LOTTERY_NUMBERS perhaps? Naming is hard, however if you get it right it makes your programs a lot more expressive.

Unused variables

If you change your plan for your program, don't forget to clean up after yourself. For example in menu_5, you're declaring two variables that you're not using (j and adder).

Expressive names

Consider how much easier this would be to read with more expressive names and meaningful constants:

//If 6 numbers match the user wins this.
//
if(compareNum == 6)
{
    printf("Congratulations, You have won the Jackpot!");
}
//If 5 numbers and the bonus matches, the user wins this.
//
if(compareNum == 5 && bonusCount == 1)
{
    printf("Congratulations, You have won a Car!");
}

becomes:

if(matched_numbers == NUM_LOTTERY_NUMBERS)
{
    printf("Congratulations, You have won the Jackpot!");
}
if(matched_numbers == (NUM_LOTTERY_NUMBERS - 1) && matched_bonus)
{
    printf("Congratulations, You have won a Car!");
}

Be Consistent

Your using different conventions for naming your variables. You've got both selectOnefirst and user_lottonumber. Pick a naming strategy and be consistent throughout the program.

Bug when matching 3 numbers and 1 bonus

You're not returning from your matches and you're not using ifs, so if compareNum == 3, both of these if clauses are matched:

if(compareNum == 3 && bonusCount == 1)
{
    printf("Congratulations, You have won a Cinema Ticket!");
}

if(compareNum == ZERO || compareNum == 1 || compareNum == 2 || compareNum == 3)
{
    printf("Hard luck, you have not won anything.");
}

You could either update your code flow, or change the final if to something like:

if(compareNum <= 2 || (compareNum == 3 && !bonusCount))
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.