Thread: Pulling my hair out , please help with this struct

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    3

    Pulling my hair out , please help with this struct

    Okay i am pulling my hair out. I am trying to get this program using the two files class_info.h and my main file to get the students averages to print out.

    Where am i missing the boat in my program to get it to call the function and work. Right now it just says press any key to continue.

    This structure stuff is hard to understand.

    Please help me.

    in class_info.h file
    Code:
    #define CLASS_SIZE 100
    
    struct student{
    char *last_name;
    int student_id;
    char grade;
    };


    in main here is what i have:
    Code:
    #include "class_info.h"
    #include <stdio.h>
    void student_average(struct student total[], int length, int id)
    {
    float sum; 
    float average; 
    int grade_count;
    grade_count=0; 
    sum=0; 
    int index=0;
    average=0.0;
    
    
    for (int k=0 ; k<length;k++)
    {
    if (total[k].student_id==id) 
    {
    grade_count++; 
    
    if( total[k].grade == 'A' ) 
    sum+=4;
    if( total[k].grade == 'B' )
    sum+=3;
    if( total[k].grade == 'C' )
    sum+=2;
    if( total[k].grade == 'D' )
    sum+=1;
    if( total[k].grade == 'F' )
    sum+=0;
    }
    }
    
    average=(sum/grade_count); 
    
    for (int l=0 ; l<length;l++) 
    {
    if (total[l].student_id==id)
    {
    index=l; 
    break;
    }
    printf("The average for each student by student id is %c\n", id);
    printf("The average for the class is %f.\n", average);
    }
    }
    int main(void)
    {
    struct student total[CLASS_SIZE];
    
    total[0].grade = 'A';
    total[0].last_name = "Bushker";
    total[0].student_id = 000001;
    
    total[1].grade = 'B';
    total[1].last_name = "johnson";
    total[1].student_id = 000002;
    
    total[2].grade = 'C';
    total[2].last_name = "Smith";
    total[2].student_id = 000003;
    
    total[3].grade = 'A';
    total[3].last_name = "Taylor";
    total[3].student_id = 000004;
    
    total[4].grade = 'B';
    total[4].last_name = "Ross";
    total[4].student_id = 000005;
    
    student_average;
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    6
    Please indent your code and then I'll take a closer look. Right now it's pretty much unreadable.

    And what is this (in main): "student_average;"?
    If you want to call a function you have to put parenthesis after the function name, and in this case also the required parameters inside the parenthesis.

  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.

  4. #4
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    You need to call the function by putting parentheses after the function name and supply it with parameters. So change
    student_average;
    to something like
    student_average(total, 5, 0);
    I don't know what the last parameter, id, is for so I just put it to 0 so you'll have to change it. It would'be been easier to see that if the code had identation.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    3
    Here is the lastest version. Still compiles good. Doesnt print student average. does my first fuction,,, average for class , but not the student one.

    Please help. I indented the code as requested. sorry for the mess.



    in class_info.h file
    Code:
    #define CLASS_SIZE 100
    
    struct student{
    char *last_name;
    int student_id;
    char grade;
    };
    Code:
    #include <stdio.h>
    
    void average(struct student total[], int length);
    void student_average(struct student total[],int ,int);
    
    int main(void)
    {
        struct student total[CLASS_SIZE];
        total[0].grade = 'A';
        total[0].last_name = "Bushker";
        total[0].student_id = 000001;
    
        total[1].grade = 'B';
        total[1].last_name = "johnson";
        total[1].student_id = 000002;
    
        total[2].grade = 'C';
        total[2].last_name = "Smith";
        total[2].student_id = 000003;
    
        total[3].grade = 'A';
        total[3].last_name = "Taylor";
        total[3].student_id = 000004;
    
        total[4].grade = 'B';
        total[4].last_name = "Ross";
        total[4].student_id = 000005;
    
        total[5].grade = 'A';
        total[5].last_name = "Ross";
        total[5].student_id = 000005;
    
        average( total, 6 );
        student_average(total, 5, 2 );
        return 0;
    }
    
    void average(student total[], int length) 
    {
        double sum=0.0,avg;
        int i;
        for( i=0;i<length;++i )
    	{
            if( total[i].grade == 'A' )
                sum+=4;
            if( total[i].grade == 'B' )
                sum+=3;
            if( total[i].grade == 'C' )
                sum+=2;
            if( total[i].grade == 'D' )
                sum+=1;
            if( total[i].grade == 'F' )
                sum+=0;
    	}
        avg = (sum/length);
        printf("The average for the class is %f.\n", avg);
    }
    void student_average(student total[], int length, int id)
    {
        float sum;      
    	float average;  
    	int grade_count;
    	grade_count=0;   
    	sum=0; 
    	int index=0;
    	average=0.0;
    
        for (int k=0 ; k<length;k++)
            {
                if (total[k].student_id==id)
                    {
                        grade_count++;
                        if( total[k].grade == 'A' )
                            sum+=4;
                        if( total[k].grade == 'B' )
                            sum+=3;
                        if( total[k].grade == 'C' )
                            sum+=2;
                        if( total[k].grade == 'D' )
                            sum+=1;
                        if( total[k].grade == 'F' )
                            sum+=0;
    				}
    		}
            average=(sum/grade_count);
            for (int l=0 ; l<length;l++)
                {
                    if (total[l].student_id==id)
                        {
                            index=l;
                            break;
    					}
    			}
    			printf("The student id, last name, and average is: %f%S%f.\n", id, average);
    }

  6. #6
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    Once you declare a struct you do not need to redeclare it as a struct. Why have one struct in a .h file and the other structs in the main c file? I try not to put structs in side a main(). Your main() should contain the call to the functions you defined and do something.

  7. #7
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    I can tell from your code that it won't compile but haven't you tried fixing the errors that the compiler spits out? It'll tell you the line it found the errors on and all that.

    Quote Originally Posted by kryptkat
    Once you declare a struct you do not need to redeclare it as a struct. Why have one struct in a .h file and the other structs in the main c file? I try not to put structs in side a main(). Your main() should contain the call to the functions you defined and do something.
    In C you can't omit the struct type-specifier when declaring structures.

  8. #8
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    read that again please.

    Once you declare a struct you do not need to redeclare it as a struct.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Perhaps I'm not seeing it -- where do you see a redeclaration?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    went back and took a closer look at that line. while reading off the compiler errors thought something different was done.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 07:13 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM