Thread: Stuck with coding while Senior Project Due!!!!

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kuro_ng View Post
    So we use calloc instead of malloc? I went to TA at school, he suggest me change to malloc but say nothing but general thing. I've been reviewed these stuff straight up couple days o_O. Thanks.
    It's not just changing a couple of characters... your use of structs is fundimentally flawed...

  2. #17
    Registered User
    Join Date
    May 2011
    Posts
    11
    thanks for reply. I will post the result when I"m done. Thanks very much.

  3. #18
    Registered User
    Join Date
    May 2011
    Posts
    11
    @CommonTater: I've fixed the dynamic memory allowance error with the right pointer. However, I'm facing "syntax error before numeric constant" in line 60. Could you guys please check it out for me. Thanks.

    Code:
    // 
    // PID: k1368475
    // Assignment: 1
    // Knightlottery Ball
    
    // Standard library declaration 
    
    #include <stdio.h>
    #include <stdlib.h>
    
    // Define numbers player and prize for matched numbers
    
    #define Played_number 6
    
        
    // Stores information for the KnightsBallLottoPlayer 
    typedef struct KnightsBallLottoPlayer {
            char firstName[20];
            char lastName[20];
            int numbers[6];
            int count;
            } KBLottoPlayer;
              
            
    enum match
    {
        three = 10,
        four = 1000,
        give = 10000,
        six = 1000000    
    };
    int main (void)
    {
        FILE * infile;
        FILE * outfile;
        infile = fopen ("input.txt", "r");
        outfile = fopen ("output.txt", "w");
        int i, j;
        
        int winners [6];
        int k;
        
        int tickets = 0;
        
        KBLottoPlayer * p;
        
    //scan the tickets
        fscanf(infile, "%d", &tickets);
        
        p = (KBLottoPlayer *)malloc(tickets*sizeof(KBLottoPlayer));
        
        
        for (i = 0; i < tickets; i++)
        {
            fscanf(infile, "%s", p[i].lastName);
            fscanf(infile, "%s", p[i].firstName);
            
            for (j = 1; j <= Played_number; j++)
            { 
                fscanf(infile, "%d", p[i].Played_number[j]);
            }
            }
            
            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 < Played_number; i++)
        {
            printf("%d ", winners[i]);
        }
        
        for (i = 0; i < j; i++)                                                                                                                                                                                                                 
        {
            for (j = 0; j < Played_number; j++)
            {
                for (k = 0; k < Played_number; k++)
                {
                    if (p[i].Played_number[j] == winners[k])
                    {
                    p[i].count++;
                    }
    
                }
            }
        } 
        for (i = 0; i , tickets; i++)
        { 
            if (p[i].count >= 3)
            printf("%s %s matched %d numbers and won $%d.\n", p[i].firstName, p[i].lastName, p[i].count);
        
        // close the file, write the file, and free memory 
        free(p);
        fclose(infile);
        fclose(outfile); 
        system("PAUSE");
        return 0;   
        }	
    }

  4. #19
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I'm not at all interested in counting lines... You know the error is on line 59 or 60 ... You know it's syntax ... shouldn't be that hard to figure out.

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't do 6[j], which is what that line 60 becomes after you substitute the define. Your struct doesn't have a member called Played_number....

  6. #21
    Registered User
    Join Date
    May 2011
    Posts
    11
    sorry CommonTater. This is my first time here, basically i'm just novice for all these. It might not hard for you, but seems abit hard for me though I"m specialize on electrical engineering. This class I have to take in order to graduate for no EE elective in summer. This is what I got so far from the help of you and tabstop and fayyazlodhi from codecall. I got stuck at
    Code:
    for (j = 0; j < numbers; j++)
    with erro fo "numbers" undeclared first use in fucntion. I thought I declare in in typedef struct already.

    this is my new revised code. Appreciate your help.

    Code:
    // PID: k1368475
    // Assignment: 1
    // Knightlottery Ball
    
    // Standard library declaration 
    
    #include <stdio.h>
    #include <stdlib.h>
    
    // Define numbers player and prize for matched numbers
    
    #define Played_number 6
    
        
    // Stores information for the KnightsBallLottoPlayer 
    typedef struct KnightsBallLottoPlayer {
            char firstName[20];
            char lastName[20];
            int numbers[6];
            int count;
            } KBLottoPlayer;
              
            
    enum match
    {
        three = 10,
        four = 1000,
        give = 10000,
        six = 1000000    
    };
    int main (void)
    {
        FILE * infile;
        FILE * outfile;
        infile = fopen ("input.txt", "r");
        outfile = fopen ("output.txt", "w");
        int i, j;
        int winners [5];
        int k;
        
        int tickets = 0;
        
        KBLottoPlayer * p;
        
    //scan the tickets
        fscanf(infile, "%d", &tickets);
        
        p = (KBLottoPlayer *)malloc(tickets*sizeof(KBLottoPlayer));
        p[i].numbers[j];
        
        for (i = 0; i < tickets; i++)
        {
            fscanf(infile, "%s", p[i].lastName);
            fscanf(infile, "%s", p[i].firstName);
            
            for (j = 1; j <= Played_number; j++)
            { 
                fscanf(infile, "%d", p[i].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 < Played_number; i++)
        {
            printf("%d ", winners[i]);
        }
        
        for (i = 0; i < j; i++)                                                                                                                                                                                                                 
        {
            for (j = 0; j < numbers; j++)
            {
                for (k = 0; k < numbers; k++)
                {
                    if (p[i].numbers[j] == winners[k])
                    {
                    p[i].count++;
                    }
    
                }
            }
        } 
        for (i = 0; i , tickets; i++)
        { 
            if (p[i].count >= 3)
            printf("%s %s matched %d numbers and won $%d.\n", p[i].firstName, p[i].lastName, p[i].count);
        
        // close the file, write the file, and free memory 
        free(p);
        fclose(infile);
        fclose(outfile); 
        system("PAUSE");
        return 0;   
        }	
    }
    Last edited by kuro_ng; 05-25-2011 at 03:01 PM.

  7. #22
    Registered User
    Join Date
    May 2011
    Posts
    11
    @tabstop. Appreciate your help tabstop. You give me a good solution for my previous error. I've been revised it so for got that I use the "#define Played_number 6" instead of "numbers 6". I've changed it.

    How could you fix the 6[j] ?

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to use the name of your array, not the number 6. The name of your array is "numbers".

  9. #24
    Registered User
    Join Date
    May 2011
    Posts
    11
    @tabstop: I got it compiled but with blank result o_O !!!!!!! I've a give a wrong structure!!!???

    Code:
    // PID: k1368475
    // Assignment: 1
    // Knightlottery Ball
    
    // Standard library declaration 
    
    #include <stdio.h>
    #include <stdlib.h>
    
    // Define numbers player and prize for matched numbers
    
    #define Played_number 6
    
        
    // Stores information for the KnightsBallLottoPlayer 
    typedef struct KnightsBallLottoPlayer {
            char firstName[20];
            char lastName[20];
            int numbers[6];
            int count;
            } KBLottoPlayer;
              
            
    enum match
    {
        three = 10,
        four = 1000,
        give = 10000,
        six = 1000000    
    };
    int main (void)
    {
        FILE * infile;
        FILE * outfile;
        infile = fopen ("input.txt", "r");
        outfile = fopen ("output.txt", "w");
        int i, j;
        int winners [6];
        int k;
        
        int tickets = 0;
        
        KBLottoPlayer * p;
        
    //scan the tickets
        fscanf(infile, "%d", &tickets);
        
        p = (KBLottoPlayer *)malloc(tickets*sizeof(KBLottoPlayer));
        p[i].numbers[j];
        
        for (i = 0; i < tickets; i++)
        {
            fscanf(infile, "%s", p[i].lastName);
            fscanf(infile, "%s", p[i].firstName);
            
            for (j = 1; j <= Played_number; j++)
            { 
                fscanf(infile, "%d", p[i].numbers[j]);
            }
            }
            
            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 < Played_number; i++)
        {
            printf("%d ", winners[i]);
        }
        
        for (i = 0; i < j; i++)                                                                                                                                                                                                                 
        {
            for (j = 0; j < p[i].numbers[j]; j++)
            {
                for (k = 0; k < p[i].numbers[j]; k++)
                {
                    if (p[i].numbers[j] == winners[k])
                    {
                    p[i].count++;
                    }
    
                }
            }
        } 
        for (i = 0; i , tickets; i++)
        { 
            if (p[i].count >= 3)
            printf("%s %s matched %d numbers and won $%d.\n", p[i].firstName, p[i].lastName, p[i].count);
        
        // close the file, write the file, and free memory 
        free(p);
        fclose(infile);
        fclose(outfile); 
        system("PAUSE");
        return 0;   
        }	
    }

  10. #25
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fscanf is just like scanf in what it expects you to provide it with for argument types:
    Code:
            for (j = 1; j <= Played_number; j++)
            { 
                fscanf(infile, "%d", p[i].numbers[j]);
    Your compiler should be complaining to you about that.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mentor for Senior Project
    By DarthNero in forum General Discussions
    Replies: 5
    Last Post: 08-31-2010, 03:11 PM
  2. HELP - Stuck on some coding
    By trueman1991 in forum C Programming
    Replies: 6
    Last Post: 10-29-2009, 12:54 PM
  3. Senior project ideas
    By Dr Spud in forum C Programming
    Replies: 3
    Last Post: 02-14-2006, 05:10 PM
  4. Replies: 12
    Last Post: 05-14-2003, 01:00 AM
  5. Senior Project: .net Framework
    By Sentaku senshi in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 11-11-2002, 10:14 AM