Accessing a struct problem

This is a discussion on Accessing a struct problem within the C Programming forums, part of the General Programming Boards category; Hey guys. I put together this program that is suppose to count up and average the grades of a class ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67

    Accessing a struct problem

    Hey guys. I put together this program that is suppose to count up and average the grades of a class of 5 people. In it, I have
    'A'=4,'B'=3, and so on. I wrote a function that I thought would average them out, but it keeps coming up with 0.00000. just like that. Can anyone see something that I am doing wrong. possibly way wrong? here is the code in the .h file:

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


    in the .c file:


    Code:
    #include "cl_info.h"
    
    void average(struct student class[]);
    
    
    int main(void)
    {
    	struct student	temp, class[CLASS_SIZE];
    
    	
    	temp.grade = 'A';
    	temp.last_name = "Bushker";
    	temp.student_id = 590017;
    
    	temp.grade = 'B';
    	temp.last_name = "Donald";
    	temp.student_id = 590117;
    
    	temp.grade = 'C';
    	temp.last_name = "Trump";
    	temp.student_id = 590217;
    
    	temp.grade = 'A';
    	temp.last_name = "Lewis";
    	temp.student_id = 590317;
    
    	temp.grade = 'B';
    	temp.last_name = "Smith";
    	temp.student_id = 590417;
    
    	
    	average(class);
    	return 0;
    }
    
    
    void average(struct student class[])
    {
    	double sum=0.0,avg;
    	int i;
    
    	for(i=0;i<5;++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/5);
    	printf("The average for the class is %f.\n", avg);
    }
    any help is greatly appreciated.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    'temp' is a single structure. You keep overwriting it.


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

  3. #3
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    should I use something like temp, temp1,temp2 and so on
    would that work?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you just use the array you made?


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

  5. #5
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    i don't think I understand. I am totally new to structures.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Code:
    void average(struct student class[])
    Legal C? And how do you go about knowing about structures, pointers, arrays, functions, and still not understand what Quzah told you.

    EDIT: Mmmkay I guess it is legal C. Didn't think you could pass arrays to functions like that in C.
    Sent from my iPadŽ

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    See your function average? See that array of structures you're passing to it? See how you access the 'grade' portion of the structure there? Do the same thing, but assign values to the array, instead of reading it from them:
    Code:
    class[0].grade = 'A';
    class[0].last_name = "Bushker";
    class[0].student_id = 590017;
    And so on...

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

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by SlyMaelstrom
    Code:
    void average(struct student class[])
    Legal C?
    Sure, what wouldn't be legal about it?


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

  9. #9
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    i'm sorry guys. I just read a new chapter about structures, and I am just trying to understand how to use them.

    if I have:

    Code:
    int main(void)
    {
          struct student    temp, class[CLASS_SIZE];
    
    temp.grade = 'A';
    temp.last_name = "Bushker";
    temp.student_id = 590017;


    i just don't understand what exactly is in the array class.
    are the members in there. Aren't these values I've assigned in the structure variable temp. Please guys, just an explanation.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Quote Originally Posted by quzah
    See your function average? See that array of structures you're passing to it? See how you access the 'grade' portion of the structure there? Do the same thing, but assign values to the array, instead of reading it from them:
    Code:
    class[0].grade = 'A';
    class[0].last_name = "Bushker";
    class[0].student_id = 590017;
    And so on...

    Quzah.
    Or use a for loop and an array of strings:
    Code:
    const char *names[] = {"Joe", "Sam", "Mike"};
    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.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have declared two things:
    Code:
    struct student temp; /* One single student, not connected in any way to anything else. */
    struct student class[ SOMESIZE ]; /* An array of 'struct student's. */
    This is just the same as any other variable declaration:
    Code:
    int temp; /* One single integer, not connected to anything else in any way. */
    int class[ SOMESIZE ]; /* An array of integers. */
    In the second code fragment, you wouldn't expect that when you change 'temp' it effects the array would you? So why would you expect it to do so with structures?


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

  12. #12
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    That worked great. THANK YOU QUZAH!!! My book didn't tell me that the
    class[i].grade is what you use to assign values to the members. I really appreciate it, man.
    I also see now how it is suppose to work.

  13. #13
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    I can see now how they all mesh with each other. I have no idea why I thought temp would take me where I needed to go. brainfart, i guess. thanks again!

  14. #14
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by Hoser83
    i'm sorry guys. I just read a new chapter about structures, and I am just trying to understand how to use them.

    if I have:

    Code:
    int main(void)
    {
          struct student    temp, class[CLASS_SIZE];
    
    temp.grade = 'A';
    temp.last_name = "Bushker";
    temp.student_id = 590017;


    i just don't understand what exactly is in the array class.
    are the members in there. Aren't these values I've assigned in the structure variable temp. Please guys, just an explanation.
    If you have an array of 10 characters. You have space for 10 character. If you have an array of 10 integers, you have space for 10 integers. If you have an array of 10 structures, you have space for 10 structures. Each of those structures have space for all of the structures members.
    Sent from my iPadŽ

  15. #15
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    Out of curiosity, if you have a class size of only 5 people, why don't you change your #define CLASS_SIZE to five, and then you could also change the condition in your for loop in the average() function to i<CLASS_SIZE

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 07:18 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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