Thread: Can someone help me with my C programming lottery code?

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    1

    Can someone help me with my C programming lottery code?

    As of right now I have my code reading from a .txt file with peoples names & there six chosen lottery numbers.

    My code ask for the file, and the winning numbers and stores the winning numbers in an array called win_nums.

    Here is where I need help. I now need to compare the winning numbers with all the peoples numbers in the .txt file. There will be someone who matches 3 numbers, 4 numbers, 5 numbers, and all 6. If someone matches with 3 they win $10 & the program prints out "first last matched 3 numbers and won $10.", 4 matches $100, 5 matches $10000, & 6 $1000000.

    If someone could help me with the code it would be greatly appreciated it.

    Here is my code as of now:

    Code:
    #include <stdio.h>
    
    typedef struct {
    char last[25];
    char first[25];
    int player_nums[6];
    }LOTTO, *LPLOTTO;
    
    int main() {
    
    //initialize variables.
    char input[50];
    int index, win_nums[6];
    
    //prompt user for file. 
    printf("Enter the name of the file with the ticket data:\n");
    scanf("%s", input);
    printf("\n");
    
    //prompt user for winning lottery numbers.
    printf("Enter the winning Lottery numbers:\n");
    for (index=0; index < 6; index++)
    scanf("%d", &win_nums[index]);
    printf("\n");
    
    //Record tickets from file. 
    int num_tix, i, j;
    LPLOTTO people;
    FILE *ifp = fopen(input, "r");
    if (ifp==0) return -1;
    fscanf(ifp,"%d",&num_tix);
    people = (LPLOTTO)malloc(sizeof(LOTTO)*num_tix);
    for (i=0; i<num_tix; i++) {
    fscanf(ifp, "%s %s", people[i].last, people[i].first);
    for (j=0;j<6;j++)
    fscanf(ifp, "%d", people[i].player_nums+j);
    }
    
    //close users file.
    fclose(ifp);
    system("PAUSE");
    return 0;
    }


    Thank you very much!
    Last edited by OvdoMan9; 09-02-2010 at 09:36 PM.

  2. #2
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Smile

    First, you're using malloc, but you did not include the stdlib.h

    Second you're missing a brace.... I'll let you find that one.

    Your code could use some indenting for readability. :-)

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM