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

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    11

    Stuck with coding while Senior Project Due!!!!

    Hey guys, I'm an electrical engineering student who was forced to take CS1 class in order to graduate cause my school doesn't provide elective class on Summer. However I took C programing for like 2,3 years ago. I forgot most of it for lack of practicing. I would really appreciate if you guys could help me out.
    Here is what I got so far.. I"m stuck with syntax problem. "expected unqualified-id before numeric constant. Really Really appreciate if you guy help me out. Thanks..

    I attached the assignment document.


    Code:
    // Name:
    // PID: k1368475
    // Assignment: 1
    // KnightlotteryBall
    
    // Standard library declaration 
    
    #include <stdio.h>
    #include <stdlib.h>
    
    // Define numbers player and prize for matched numbers
    
    #define Played_number 6
    #define 3numbers 10;
    #define 4numbers 1000;
    #define 5numbers 10000;
    #define 6numbers 1000000;
    
        
    // Stores information for the KnightsBallLottoPlayer 
    typedef struct KnightsBallLottoPlayer {
            char firstName[20];
            char lastName[20];
            int numbers[6];
            } KBLottoPlayer;
    
    int main (void)
    {
        FILE * infile;
        FILE * outfile;
        infile = fopen ("input.txt", "r");
        outfile = fopen ("output.txt", "w");
        int i, j;
        int winners [6];
        int h;
        int* ptr;
        int tickets = 0;
    
        fscanf(infile, "%d", &tickets);
    
    struct KnightsBallLottoPlayer KBLottoPlayer[tickets];
        
        ptr =(int*)calloc(tickets, sizeof(int));
        
        for (i = 0; i < tickets; i++)
        {
            fscanf(infile, "%s", &KBLottoPlayer[i].lastName);
            fscanf(infile, "%s", &KBLottoPlayer[i].firstName);
            
            for (j = 1; j <= Played_number; j++)
            { fscanf(infile, "%d", &KBLottoPlayer[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 (h = 0; h < Played_number; h++)
                {
                    if (KBLottoPlayer[i].nums_played[j] == winners[h])
                    {
    
                    }
                }
            }
        } 
    
     
        fclose(infile);
        fclose(outfile); 
        system("PAUSE");
        return 0;   
    }
    Attached Images Attached Images
    Last edited by kuro_ng; 05-25-2011 at 06:44 AM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    #define 3numbers 10;
    #define 4numbers 1000;
    #define 5numbers 10000;
    #define 6numbers 1000000;
    Never put an ";" semicolon in a define statement.
    I would also not start them (define macro) with a number.

    Code:
    // Stores information for the KnightsBallLottoPlayer 
    typedef struct KnightsBallLottoPlayer {
            char firstName[20];
            char lastName[20];
            int numbers[6];
            } KBLottoPlayer;
    Code:
    struct KnightsBallLottoPlayer KBLottoPlayer[tickets];
    Do NOT reuse the identifier KBLottoPlayer twice for two different things.

    Move the first line of real code "fopen" and "fscanf" after the variable section (required by some C Compilers).
    Code:
    fscanf(infile, "%d", &tickets);
    Tim S.
    Last edited by stahta01; 05-25-2011 at 06:58 AM.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    11
    Thanks Tim for the help. I've change my code using malloc instead. Still have the error


    Code:
    // 
    
    
    Name: 
    // 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
    #define 3numbers 10
    #define 4numbers 1000
    #define 5numbers 10000
    #define 6numbers 1000000
    
        
    // Stores information for the KnightsBallLottoPlayer 
    typedef struct KnightsBallLottoPlayer {
            char firstName[20];
            char lastName[20];
            int numbers[6];
            } KBLottoPlayer;
    
    int main (void)
    {
        FILE * infile;
        FILE * outfile;
        infile = fopen ("input.txt", "r");
        outfile = fopen ("output.txt", "w");
        int i, j;
        int KBLottoPlayer;
        int winners [6];
        int k;
        int* p;
        int tickets = 0;
        
    //scan the tickets
        fscanf(infile, "%d", &tickets);
        
        p = (int *)malloc(KBLottoPlayer*sizeof(int));
        
        for (i = 0; i <KBLottoPlayer; i++)
        {
            fscanf(infile, "%s", KBLottoPlayer[i].lastName);
            fscanf(infile, "%s", KBLottoPlayer[i].firstName);
            
            for (j = 1; j <= Played_number; j++)
            { fscanf(infile, "%d", KBLottoPlayer[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; h++)
                {
                    if (KBLottoPlayer[i].nums_played[j] == winners[k])
                    {
    
                }
            }
        } 
    
     
        fclose(infile);
        fclose(outfile); 
        system("PAUSE");
        return 0;   
    }

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Valid identifier does not start with number.

    Code:
    #define 3numbers 10
    #define 4numbers 1000
    #define 5numbers 10000
    #define 6numbers 1000000

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    11
    Quote Originally Posted by Bayint Naung View Post
    Valid identifier does not start with number.

    Code:
    #define 3numbers 10
    #define 4numbers 1000
    #define 5numbers 10000
    #define 6numbers 1000000
    Thanks Bayint Naung. I change it to enum so it would be fine with the define thing. However I still getting the error after all

    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];
    } KBLottoPlayer;
    
    
    enum match
    {
    three = 10,
    four = 1000,
    five= 10000,
    six = 1000000
    };
    
    
    int main (void)
    {
    FILE * infile;
    FILE * outfile;
    infile = fopen ("input.txt", "r");
    outfile = fopen ("output.txt", "w");
    int i, j;
    int KBLottoPlayer;
    int winners [6];
    int h;
    int* p;
    int tickets = 0;
    
    //scan the tickets
    fscanf(infile, "%d", &tickets);
    
    p = (int *)malloc(KBLottoPlayer*sizeof(int));
    
    for (i = 0; i < tickets; i++)
    {
    fscanf(infile, "%s", KBLottoPlayer[i].lastName);
    fscanf(infile, "%s", KBLottoPlayer[i].firstName);
    
    for (j = 1; j <= Played_number; j++)
    { fscanf(infile, "%d", KBLottoPlayer[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 (h = 0; h < Played_number; h++)
    {
    if (KBLottoPlayer[i].nums_played[j] == winners[h])
    {
    
    }
    }
    }
    
    
    fclose(infile);
    fclose(outfile);
    system("PAUSE");
    return 0;
    }
    Last edited by kuro_ng; 05-25-2011 at 10:00 AM.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You are most likely getting the error on this line:
    Code:
    int winners [6];
    Notice the space between winners and the array size.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kuro_ng
    I change it to enum so it would be fine with the define thing. However I still getting the error after all
    You should indent your code and tell us what is the exact error, including which line was indicated by the compiler.

    Quote Originally Posted by claudiu
    Notice the space between winners and the array size.
    Whitespace is not significant in this context, so if the compiler reports an error, you can report a real live compiler bug.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What happened to your indentation?
    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.

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    OP: post compiler error and post your code with line number!

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I wonder if it's because they copy/pasted some pre-crafted answer from another forum?

    There are several places where google found it, say
    Stuck with coding while Senior Project Due!!!!
    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.

  11. #11
    Registered User
    Join Date
    May 2011
    Posts
    11
    @claudiu: thanks for the help. but it is ok with the space though
    @laserlight and Bayint sorry I forgot about it:
    @Salem: enum is a same way to do that
    I post 2 place to get more help. here and codecall. I dont copy and paste. I've try to do this since 2 days a go... sorry for multiple posts.
    this is the code
    I got error at line 52 "fscanf(infile, "%s", KBLottoPlayer[i].lastName);"

    with syntax error: invalid type "int[int]' for array subscript
    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,
        five= 10000,
        six = 1000000    
    };
    int main (void)
    {
        FILE * infile;
        FILE * outfile;
        infile = fopen ("input.txt", "r");
        outfile = fopen ("output.txt", "w");
        int i, j;
        int KBLottoPlayer;
        int winners [6];
        int k;
        int* p;
        int tickets = 0;
        
    //scan the tickets
        fscanf(infile, "%d", &tickets);
        
        p = (int *)malloc(KBLottoPlayer*sizeof(int));
        
        for (i = 0; i < tickets; i++)
        {
            fscanf(infile, "%s", KBLottoPlayer[i].lastName);
            fscanf(infile, "%s", KBLottoPlayer[i].firstName);
            
            for (j = 1; j <= Played_number; j++)
            { fscanf(infile, "%d", KBLottoPlayer[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; h++)
                {
                    if (KBLottoPlayer[i].nums_played[j] == winners[k])
                    {
                    KBLottoPlayer[i].count++;
                    }
    
                }
            }
        } 
        for (i = 0; i , tickets; i++0
        { 
            if (KBLottoPlayer[i].count >= 3)
            printf("%s %s matched %d numbers and won $%d.\n", KBLottoPlayer[i].firstName, KBLottoPlayer[i].lastName, KBLottoPlayer[i].count);
     
        fclose(infile);
        fclose(outfile); 
        system("PAUSE");
        return 0;   
    }
    Last edited by kuro_ng; 05-25-2011 at 10:35 AM.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You've defined a struct KBLottoPlayer but in your code KBLottoPlayer is an integer... not a struct.

    You need to allocate actual space for the structs...
    Code:
    KBLottoPlayer *Players = calloc(100,sizeof(KBLottoPlayers));
    Now you can use Players[index]->firstName etc in your code.

  13. #13
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Yes, taking a second look at it now, this looks so fake and stolen it is not even worth my time; and posting on more than one forum doesn't get you MORE help but LESS help, it just wastes MORE time for MORE people.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  14. #14
    Registered User
    Join Date
    May 2011
    Posts
    11
    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.

  15. #15
    Registered User
    Join Date
    May 2011
    Posts
    11
    claudiu: if you look at the username: kuro_ng. it is me!!!

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