1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help with program

Discussion in 'C' started by snorflack, Sep 3, 2010.

  1. snorflack

    snorflack New Member

    Sep 3, 2010
    Likes Received:
    Trophy Points:
    I cant get this program to work I can get it to read in the file and enter the winning numbers but it just ends after it. the program is supposed to take in a file (input.txt) and make it into a list of structs with peoples names and lotto numbers in it and then get numbers from the user and compare them with the numbers in the structs and if there is any winners they win certain amounts of money. im using dev c.

    #define NUMBERS_PLAYED 6
    enum MATCHED
        NONE = 0,
        THREE = 10,
        FOUR = 1000,
        FIVE = 10000,
        SIX = 1000000    
    struct player
        char last[19];
        char first[19];
        int nums_played[6];
        int count;
        int prize;
    int main ()
        FILE* ifp;
        char input[1024];
        int winners[6];
        int i, j;
        int k;
        int ticketsbought = 0;
        struct player* player = 0;
        //Ask user for the name of the file to read from
        printf("Please enter the name of the file with the ticket data. \n");
        //Read in the file to read from
        fgets(input, sizeof(input), stdin);
        if( input[strlen(input)-1] == '\n')
            input[strlen(input)-1] = '\0';
        //Open file for reading
        ifp = fopen(input
        , "r");
        //The first line will contain a single integer n, the total number of 
        //tickets bought. Now we will read in that first line
        fscanf(ifp, "%d ", &ticketsbought); 
        player = (struct player *)malloc(ticketsbought *sizeof(player));
        //The first line will contain the last name of the ticket buyer, followed by 
        //a space, followed by the first name of ticket buyer
        for (i = 0; i < ticketsbought; i++)
            fscanf(ifp, "%s ", player[i].last);
            fscanf(ifp, "%s ", player[i].first);
            for (j = 0; j < NUMBERS_PLAYED; j++)
                fscanf(ifp, "%d ", &player[i].nums_played[j]);
        //Close the input.txt file
        //Ask the user for the winning combination of numbers
        printf("Please enter the winning lottery numbers:\n");
        scanf("%d %d %d %d %d %d", &winners[0], &winners[1], &winners[2], &winners[3], &winners[4], &winners[5]);
        for (i = 0; i < ticketsbought; i++)
            player[i].count = 0;
            for (j = 0; j < 6; j++)
                for (k = 0; k < 6; k++)
                    if (player[i].nums_played[j] == winners[k])
            if (player[i].count == 3)
                player[i].prize = THREE;
            else if (player[i].count == 4)
                player[i].prize = FOUR;
            else if (player[i].count == 5)
                player[i].prize = FIVE;
            else if (player[i].count == 6)
                player[i].prize = SIX;
                player[i].prize = NONE;
            if( player[i].count > 0)
                printf("%s %s matched %d numbers and won $%d \n",
                    player[i].first, player[i].last, player[i].count, player[i].prize);
        return 0;   

Share This Page