Student average program rookie in need of help

This is a discussion on Student average program rookie in need of help within the C Programming forums, part of the General Programming Boards category; Need some baby steps to understand this structure bussiness. Here is my beginnings of code. I cant even make it ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    51

    Student average program rookie in need of help

    Need some baby steps to understand this structure bussiness. Here is my beginnings of code. I cant even make it past the compiler. My first struct statement says "missing code tag". whats wrong with the statement. I am working exactly the code as it is stated in the book and adding too , as instructed in the excercise. The statement struct is exactly like that in the book. So this is why i am confused to why i am getting a compile error.

    The end game goal is to use the partial program in the book and build onto it making it calculate each students average.

    thanks in advance for your help.

    Code:
    #include "class_info.h"
    #include <stdio.h>
    
    int main(void)
    {
       struct student           class[CLASS_SIZE];
    
    
        class[0].grade = 'A';
        class[0].last_name = "Walker";
        class[0].student_id = 590017;
    
        class[1].grade = 'B';
        class[1].last_name = "Smith";
        class[1].student_id = 590118;
    
        class[2].grade = 'C';
        class[2].last_name = "jones";
        class[2].student_id = 590219;
    
        class[3].grade = 'A';
        class[3].last_name = "Bubba";
        class[3].student_id = 590320;
    
        class[4].grade = 'B';
        class[4].last_name = "johns";
        class[4].student_id = 590421;
    
        class[5].grade = 'C';
        class[5].last_name = "Walker";
        class[5].student_id = 590017;
    
        class[6].grade = 'D';
        class[6].last_name = "Smith";
        class[6].student_id = 590118;
    
        class[7].grade = 'B';
        class[7].last_name = "jones";
        class[7].student_id = 590219;
    
        class[8].grade = 'A';
        class[8].last_name = "Bubba";
        class[8].student_id = 590320;
    
        class[9].grade = 'C';
        class[9].last_name = "johns";
        class[9].student_id = 590421;
    
        average(class);
        return 0;
    }
    
    
    void average(struct student class[])
    {
        double sum=0.0,avg;
        int i;
        int length = sizeof(class)/sizeof(student);
    
        for( i=0;i<length;++i )
        {
            if( class[i].grade == 'A' );
            sum+=4;
            if( class[i].grade == 'B' );
            sum+=3;
            if( class[i].grade == 'C' );
            sum+=2;
            if( class[i].grade == 'D' );
            sum+=1;
            if( class[i].grade == 'F' );
            sum+=0;
    
        }
        avg = (sum/length);
        printf("The average for the class is %f.\n", avg);
    }

    here is my
    class_info.h file
    Code:
    #include <stdio.h>
    
    
    #define CLASS_SIZE  100
    
    struct student{
    	char	*last_name;
    	int		student_id;
    	char	grade;
    };

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    If you're using a C++ compiler it might complain about your usage of the variable name class.

    What line is the error on? This one?
    Code:
    struct student           class[CLASS_SIZE];
    You have extra semicolons here; get rid of them:
    Code:
            if( class[i].grade == 'A' );
            sum+=4;
            if( class[i].grade == 'B' );
            sum+=3;
            if( class[i].grade == 'C' );
            sum+=2;
            if( class[i].grade == 'D' );
            sum+=1;
            if( class[i].grade == 'F' );
            sum+=0;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    class[0].last_name = "Walker";
    strcpy()...
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    Yes that is the line that is getting the error. I am using visio studio 2005 as my compiler.

    So how do i fix it?

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Mshock
    Yes that is the line that is getting the error. I am using visio studio 2005 as my compiler.

    So how do i fix it?
    Be sure to set it to C not C++.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > class[0].last_name = "Walker";
    > strcpy()...
    No, the assignment is correct, it is a char* in the struct.

    > int length = sizeof(class)/sizeof(student);
    This only works when the array is in scope.
    If all you have is a pointer to the array (what you get when you pass the array as a parameter), then this calculation does not do what you want it to.

    Try
    void average(struct student class[], int length)

    And call it with
    average( class, 10 );

    > Yes that is the line that is getting the error
    For the benefit of those who do not have your compiler, or can't be bothered to copy all your code to a project just to find out, say what your error messages are.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    Okay how do i change the compiler in Visio Studio .net 2003 to use only the c compiler instead of C++. Or any alternate solutions to the error i get for the struct command with class.

    Second questions is i added the two items metioned to the program here is the revised code. how does it look now.

    Code:
    #include "class_info.h"
    #include <stdio.h>
    
    int main(void)
    {
       struct student           classs[CLASS_SIZE];
    
    
        class[0].grade = 'A';
        class[0].last_name = "Walker";
        class[0].student_id = 590017;
    
        class[1].grade = 'B';
        class[1].last_name = "Smith";
        class[1].student_id = 590118;
    
        class[2].grade = 'C';
        class[2].last_name = "jones";
        class[2].student_id = 590219;
    
        class[3].grade = 'A';
        class[3].last_name = "Bubba";
        class[3].student_id = 590320;
    
        class[4].grade = 'B';
        class[4].last_name = "johns";
        class[4].student_id = 590421;
    
        class[5].grade = 'C';
        class[5].last_name = "Walker";
        class[5].student_id = 590017;
    
        class[6].grade = 'D';
        class[6].last_name = "Smith";
        class[6].student_id = 590118;
    
        class[7].grade = 'B';
        class[7].last_name = "jones";
        class[7].student_id = 590219;
    
        class[8].grade = 'A';
        class[8].last_name = "Bubba";
        class[8].student_id = 590320;
    
        class[9].grade = 'C';
        class[9].last_name = "johns";
        class[9].student_id = 590421;
    
        average( class, 10 );
    
        return 0;
    }
    
    
    void average(struct student class[], int length)
    {
        double sum=0.0,avg;
        int i;
        int length = sizeof(class)/sizeof(student);
    
        for( i=0;i<length;++i )
        {
            if( class[i].grade == 'A' ) 
            sum+=4;
            if( class[i].grade == 'B' ) 
            sum+=3;
            if( class[i].grade == 'C' ) 
            sum+=2;
            if( class[i].grade == 'D' ) 
            sum+=1;
            if( class[i].grade == 'F' ) 
            sum+=0;
    
        }
        avg = (sum/length);
        printf("The average for the class is %f.\n", avg);
    }
    Last edited by Salem; 05-10-2006 at 01:02 AM. Reason: snip email address

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    this is the error i get, sorry for not posting this earlier

    (9): error C2332: 'class' : missing tag name
    (9): warning C4094: untagged 'class' declared no symbols

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Mshock
    this is the error i get, sorry for not posting this earlier

    (9): error C2332: 'class' : missing tag name
    (9): warning C4094: untagged 'class' declared no symbols
    As ive mentioned be sure to set it to C I bet this is a problem cause class keyword is reserved under C++.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    been looking for where to change it in the program. So far dont see an option. Still looking

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    This is my output with gcc

    Code:
    rookie.c:1:24: class_info.h: No such file or directory
    rookie.c: In function `main':
    rookie.c:6: error: `CLASS_SIZE' undeclared (first use in this function)
    rookie.c:6: error: (Each undeclared identifier is reported only once
    rookie.c:6: error: for each function it appears in.)
    rookie.c:9: error: `class' undeclared (first use in this function)
    rookie.c:49: warning: implicit declaration of function `average'
    rookie.c:6: warning: unused variable `classs'
    rookie.c: At top level:
    rookie.c:55: warning: `struct student' declared inside parameter list
    rookie.c:55: warning: its scope is only this definition or declaration, which is probably not what you want
    rookie.c:56: warning: type mismatch with previous implicit declaration
    rookie.c:49: warning: previous implicit declaration of `average'
    rookie.c:56: warning: `average' was previously implicitly declared to return `int'
    rookie.c: In function `average':
    rookie.c:59: warning: declaration of `length' shadows a parameter
    rookie.c:59: error: `student' undeclared (first use in this function)
    rookie.c:63: error: invalid use of undefined type `struct student'
    rookie.c:63: error: dereferencing pointer to incomplete type
    rookie.c:65: error: invalid use of undefined type `struct student'
    rookie.c:65: error: dereferencing pointer to incomplete type
    rookie.c:67: error: invalid use of undefined type `struct student'
    rookie.c:67: error: dereferencing pointer to incomplete type
    rookie.c:69: error: invalid use of undefined type `struct student'
    rookie.c:69: error: dereferencing pointer to incomplete type
    rookie.c:71: error: invalid use of undefined type `struct student'
    rookie.c:71: error: dereferencing pointer to incomplete type
    rookie.c:77:2: warning: no newline at end of file
    Man just a tip, take smething like Dev-Cpp or MingW
    Last edited by Maragato; 05-09-2006 at 08:48 PM.

  12. #12
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Maragato, your gcc output is incorrect. Read the first line: "class_info.h: no such file..." Try saving the file (OP posts it above), and then list a compiler output. Such as this one: (I called his main file "ci.c" by the way... I'm a lazy typist...)) (gcc 3.4.2, MinGW)
    Code:
    ci.c: In function `main':
    ci.c:9: error: `class' undeclared (first use in this function)
    ci.c:9: error: (Each undeclared identifier is reported only once
    ci.c:9: error: for each function it appears in.)
    ci.c:49: warning: implicit declaration of function `average'
    ci.c:6: warning: unused variable `classs'
    ci.c: At top level:
    ci.c:56: error: conflicting types for 'average'
    ci.c:49: error: previous implicit declaration of 'average' was here
    ci.c: In function `average':
    ci.c:59: error: 'length' redeclared as different kind of symbol
    ci.c:55: error: previous definition of 'length' was here
    ci.c:59: error: `student' undeclared (first use in this function)
    ...which is hopefully a better represntation of the problem.

    Reading that, in main(), you've got a variable called "classs" but not "class".
    You never prototyped funtion average.
    And finally, you have two length variables in average. (One which uses student, which isn't defined. If you're following the suggestions given, you probably want to remove the one involving student.)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  13. #13
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Cactus_Hugger
    Maragato, your gcc output is incorrect. Read the first line: "class_info.h: no such file..." Try saving the file (OP posts it above), and then list a compiler output. Such as this one: (I called his main file "ci.c" by the way... I'm a lazy typist...)) (gcc 3.4.2, MinGW)
    Code:
    ci.c: In function `main':
    ci.c:9: error: `class' undeclared (first use in this function)
    ci.c:9: error: (Each undeclared identifier is reported only once
    ci.c:9: error: for each function it appears in.)
    ci.c:49: warning: implicit declaration of function `average'
    ci.c:6: warning: unused variable `classs'
    ci.c: At top level:
    ci.c:56: error: conflicting types for 'average'
    ci.c:49: error: previous implicit declaration of 'average' was here
    ci.c: In function `average':
    ci.c:59: error: 'length' redeclared as different kind of symbol
    ci.c:55: error: previous definition of 'length' was here
    ci.c:59: error: `student' undeclared (first use in this function)
    ...which is hopefully a better represntation of the problem.

    Reading that, in main(), you've got a variable called "classs" but not "class".
    You never prototyped funtion average.
    And finally, you have two length variables in average. (One which uses student, which isn't defined. If you're following the suggestions given, you probably want to remove the one involving student.)
    Thanks by pointing the header mistake.

    Oki man Ive re-writen and got a working copy take a look on the way you initialize your name field.
    You also still programming in c++ cause you need typedef struct foo...
    Last edited by Maragato; 05-09-2006 at 09:49 PM.

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    Thanks for the help with the switching the program over to C programing. Here are the final errors i am getting

    (49): error C2143: syntax error : missing ';' before 'type'
    (56): error C2449: found '{' at file scope (missing function header(77): error C2059: syntax error : '}'


    After i get this function working I need to have the program print out the student average. Right now i am just trying to understand how to make this structure stuff work to just do the class average , goal is student average.

    Am i even close to getting this thing running. Let me know your suggestions.

    thanks in advance for your help

    main.c
    Code:
    #include "class_info.h"
    #include <stdio.h>
    
    int main(void)
    {
       struct student           class[CLASS_SIZE];
    
    
        class[0].grade = 'A';
        class[0].last_name = "Walker";
        class[0].student_id = 590017;
    
        class[1].grade = 'B';
        class[1].last_name = "Smith";
        class[1].student_id = 590118;
    
        class[2].grade = 'C';
        class[2].last_name = "jones";
        class[2].student_id = 590219;
    
        class[3].grade = 'A';
        class[3].last_name = "Bubba";
        class[3].student_id = 590320;
    
        class[4].grade = 'B';
        class[4].last_name = "johns";
        class[4].student_id = 590421;
    
        class[5].grade = 'C';
        class[5].last_name = "Walker";
        class[5].student_id = 590017;
    
        class[6].grade = 'D';
        class[6].last_name = "Smith";
        class[6].student_id = 590118;
    
        class[7].grade = 'B';
        class[7].last_name = "jones";
        class[7].student_id = 590219;
    
        class[8].grade = 'A';
        class[8].last_name = "Bubba";
        class[8].student_id = 590320;
    
        class[9].grade = 'C';
        class[9].last_name = "johns";
        class[9].student_id = 590421;
    
    	void average( class, 10 )
    
        return 0;
    }
    
    
    void average(struct student     class[], int length);
    {
        double sum=0.0,avg;
        int i;
        int length = sizeof(class)/sizeof(student);
    
        for( i=0;i<length;++i )
        {
            if( class[i].grade == 'A' ) 
            sum+=4;
            if( class[i].grade == 'B' ) 
            sum+=3;
            if( class[i].grade == 'C' ) 
            sum+=2;
            if( class[i].grade == 'D' ) 
            sum+=1;
            if( class[i].grade == 'F' ) 
            sum+=0;
    
        }
        avg = (sum/length);
        printf("The average for the class is %f.\n", avg)
    }
    return 0

    here is my class_info.h file
    Code:
    #include <stdio.h>
    
    
    #define CLASS_SIZE  100
    
    struct student{
    	char	*last_name;
    	int		student_id;
    	char	grade;
    };

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Database assignment is Killing me!
    By Boltrig in forum C Programming
    Replies: 2
    Last Post: 11-29-2007, 02:56 AM
  2. GPA Program Problems
    By Clint in forum C Programming
    Replies: 3
    Last Post: 04-28-2005, 10:45 AM
  3. I have a function and I need multiple averages returned
    By tommy69 in forum C++ Programming
    Replies: 20
    Last Post: 04-13-2004, 11:45 AM
  4. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM
  5. C++ Program that Calculates average of three scores
    By dccog in forum C++ Programming
    Replies: 1
    Last Post: 03-27-2002, 11:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21