draconias22 0 Newbie Poster

Ok, so I need some help. I've already submitted this for a grade, but I just want to know how to do it better.

I don't know how to create functions for this program due to the structs and pointers. I would appreciate any help.

Essentially I want to create a function for allocating memory for the structs and then another function for comparing the winning lotto numbers to the numbers read into from file.

Program purpose: Read in from a file designated by the user, the user enters the winning lottery numbers, and then the program compares the winning numbers to each ticket's numbers and then prints off the owner of each ticket, how many numbers they matched and the amount of money they won (as long as they matched 3 or more numbers).

Here is a sample file input:

5
Llewellyn Mark
1 15 19 26 33 46
Young Brian
17 19 33 34 46 47
Cazalas Jonathan
1 4 9 16 25 36
Siu Max
17 19 34 46 47 48
Balci Murat
5 10 17 19 34 47

Here is the code:

#include <stdio.h>
#define NUMBALLSDRAWN 6
#define NUMBALLSPLAYED 6
#define MAXNUMBERS 53

enum lottery
{
    NUMMATCHED_3 = 10,
    NUMMATCHED_4 = 100,
    NUMMATCHED_5 = 10000,
    NUMMATCHED_6 = 1000000,
};

struct lotto
{
    char first_name [19];
    char last_name [19];
    int player_numbers[NUMBALLSDRAWN];
};


int main(void)
{
    FILE* fin;//pointer to file user wants to open
    char fname[256]; //stores file name read into by user
    int ticketsbought; //integer read from file indicating number of tickets bought
    int winningnumbers[NUMBALLSDRAWN];//integer array storing winning lottery numbers input by user
    int numbersmatched;//used as a counter when comparing winning numbers to players 
                       //numbers to see how many numbers the player matched to the winning numbers
    struct lotto *players_ticket;//pointer 
    int i; //for-loop index
    int j; //for-loop index
    int k;//for-loop index
    
    printf("Enter the name of the file with the ticket data.\n");
    scanf(" %s", fname);
    
    fin = fopen(fname, "r");
    
    if(fin == NULL) //if an error occurs and file can't be opened
    {
        printf("\aError: That file cannot be opened\n");
        system ("PAUSE");
        return 0;
    }
    
    fscanf(fin, "%d", &ticketsbought);//scanning in first number of file; necessary to allocate memory for struct
    
    players_ticket = (struct lotto *)malloc(sizeof(struct lotto)*ticketsbought);//allocating memory for struct lotto
    
    if(players_ticket == NULL)//Error if memory allocation failed
    {
        printf("\aError: Memory Allocation Failed\n");
        return 0;
    }
    
    for(i = 0; i < ticketsbought; i++)/*Scan in the ticket information for the numbers 
                                        of tickets bought(1st number scanned in from the file)*/
    {
        fscanf(fin, " %s" " %s", &players_ticket[i].last_name, &players_ticket[i].first_name); // scan in the name associated with the ticket
        
        for(j=0; j<NUMBALLSDRAWN; j++)//scan in the numbers on the ticket.  Amount of numbers is defined by NUMBALLSDRAWN.
            fscanf(fin, "%d", &players_ticket[i].player_numbers[j]);
    }
    fclose(fin);//close the file read in by the user
       
    puts("Enter the winning Lottery numbers");
    
    for(i=0; i<NUMBALLSDRAWN; i++)//scan in each winning number into an int array
    {    
        scanf(" %d", &winningnumbers[i]);
        
        if(winningnumbers[i] < 1 || winningnumbers[i] > MAXNUMBERS)//error statement if user doesn't enter a valid winning lottery number
        {
            printf("That's not a valid lotto number.\n");
            break;
        }
    }
    for(i=0; i<ticketsbought; i++)//loop through the tickets
    {
        for(j=0; j<NUMBALLSPLAYED; j++)//loop through each players numbers
        {
            for(k=0; k<NUMBALLSPLAYED; k++)//loop through each winning number, while comparing to players jth number
            {                               
                if(players_ticket[i].player_numbers[j] == winningnumbers[k])//if a winning number is found among a players' numbers, count it
                {
                    numbersmatched++;//counter
                    break;                    
                }
            }
        }        
        
        switch(numbersmatched)//If the player has 3 to 6 matched numbers, they enter this switch statement that prints off the information
        {
            case 3://matches 3 numbers
                printf("%s %s matched %d numbers and won $%d.\n",players_ticket[i].first_name, 
                        players_ticket[i].last_name, numbersmatched, NUMMATCHED_3);
                break;
            case 4://matches 4 numbers
                printf("%s %s matched %d numbers and won $%d.\n",players_ticket[i].first_name, 
                        players_ticket[i].last_name, numbersmatched, NUMMATCHED_4);
                break;
            case 5://matches 5 numbers
                printf("%s %s matched %d numbers and won $%d.\n",players_ticket[i].first_name, 
                        players_ticket[i].last_name, numbersmatched, NUMMATCHED_5);
                break;
            case 6: //matches 6 numbers
                printf("%s %s matched %d numbers and won $%d.\n",players_ticket[i].first_name, 
                        players_ticket[i].last_name, numbersmatched, NUMMATCHED_6);
                break;
        }
        numbersmatched = 0;//reset the number of matched numbers to zero for the next ticket to be compared before beginning loop again
    }
    
    free(players_ticket);//free the malloc'ed memory
    
    system("PAUSE");
    return 0;
}