C program problem with structures and functions

I have to write a function that will print the average for students in a class and I have to use a structure and the member operator to complete it.  Well, I can't just write the function because I want to be able to test it to make sure that it will work and now I'm stuck.  I'm currently getting some errors that I have no idea how to fix:  Any help would be greatly appreciated.  

error C2040: '==' : 'char [5]' differs in levels of indirection from 'int'
 error C2446: '==' : no conversion from 'int' to 'char *'
(Header file)
#include <stdio.h>
 
#define CLASS_SIZE 1
 
 
struct student {
	char last_name[20];
	int  student_id;
	char grade[5];
	char overall;
}; 
(Main file)
#include "cl_info.h"
 
int average(struct student group);
 
int main(void)
{
	struct student group[CLASS_SIZE];
	int k;
	int number = 0;
	
	
 
 
 
	
	
	for (k = 0; k < CLASS_SIZE; ++k)
	{
		printf("\nEnter name: ");
		scanf(" %s", &group[k].last_name); 
		printf("\nEnter 5 grades: ");
			for (k = 0; k < 5; ++k)
				scanf(" %c", &group[k].grade);
		
	}
	for (k = 0; k < CLASS_SIZE; ++k)
	printf("\n%s\n\n", group[k].last_name);	
 
	average;
 
 
}
(Function)
#include "cl_info.h"
 
int average(struct student group[])			/*group was used in place of class*/
{
	int i;
	int j;
	int cnt;				/*Will count the total points per grade*/
	int average = 0;		/*The final average per student*/
	int how_many;			/*How many grades were entered*/
 
	/*The function will pass through the array of students*/
 
	for (j = 0; j < CLASS_SIZE; ++j)
	{
	/*For each student the function will ask for the number of grades*/
 
		printf("How many grades would you like to average for %s?", group[j].last_name);
		scanf("%d", how_many);
	}
	/*For every number entered grade to be averaged this part will count and add to it depending on the grade.*/
	/*The points will depend on the letter grade.*/
 
			for (i = 0; i < how_many; ++i)
			{
				if (group[i].grade == 'a' || 'A')
				cnt += 4;
				if (group[i].grade == 'b' || 'B')
				cnt += 3;
				if (group[i].grade == 'c' || 'C')
				cnt += 2;
				if (group[i].grade == 'd' || 'D')
				cnt += 1;
			
			}
	
	/*The math will be calculated and returned for each student.*/
 
	average = cnt/how_many;
 
	printf("\nThe average for the %d grades is %d.\n", how_many, average);
 
	
	return 1;
 
}

Open in new window

pmo977Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
There is a conceptual error in your nested loop:

int j,g;  /* you will need an extra variable here */

for (k = 0; k < CLASS_SIZE; ++k)
      {
            printf("\nEnter name: ");
            scanf(" %s", &group[k].last_name);
            printf("\nEnter 5 grades: ");
                  for (j = 0; j < 5; ++j)    /* use a different counter instead of k */
                        scanf(" %c", &(group[k].grade[j]));   /* pay attention to this */
            
      }

This loop has a similar problem:

            for (i = 0; i < how_many; ++i)
      {
            if (group[j].grade[i] == 'a' || group[j].grade[i] =='A')
                  cnt += 4;
            if (group[j].grade[i] == 'b' || group[j].grade[i] =='B')
                  cnt += 3;
            if (group[j].grade[i] == 'c' || group[j].grade[i] =='C')
                  cnt += 2;
            if (group[j].grade[i] == 'd' || group[j].grade[i] =='D')
                  cnt += 1;
      }

but can be simplified as:

      for (i = 0; i < how_many; ++i)
      {
                         switch (toupper(group[j].grade[i]))
                         {
                                 case 'A':
                  cnt += 4; break;
                                 case 'B':
                  cnt += 3; break;
                                 case 'C':
                  cnt += 2; break;
                                 case 'D':
                  cnt += 1; break;
               }
      }
0
 
Jaime OlivaresSoftware ArchitectCommented:
Have you tried?
0
 
pmo977Author Commented:
I did try and I have another question.  Everything is working except now if I increase the number of CLASS_SIZE to more than one it will only return the average for the first set of grades entered.  I don't have access to my code right now but I can attach it later.  This works no problem for a class size of 1:
   
for (i = 0; i < how_many; ++i)
      {
            if (group[j].grade[i] == 'a' || group[j].grade[i] =='A')
                  cnt += 4;
            if (group[j].grade[i] == 'b' || group[j].grade[i] =='B')
                  cnt += 3;
            if (group[j].grade[i] == 'c' || group[j].grade[i] =='C')
                  cnt += 2;
            if (group[j].grade[i] == 'd' || group[j].grade[i] =='D')
                  cnt += 1;
      }
 I'm going to try the simplified solution you provided and see if the works.  Thanks for helping!
0
Managing Security & Risk at the Speed of Business

Gartner Research VP, Neil McDonald & AlgoSec CTO, Prof. Avishai Wool, discuss the business-driven approach to automated security policy management, its benefits and how to align security policy management with business processes to address today's security challenges.

 
pmo977Author Commented:
Ok, I've made a few changes and I'm still only getting the average for the first set of grades I enter.  If I keep everything in the main it will work but when I try to break it up I have these problems.  Can you help me!
(Header file)
#include <stdio.h>
#include <ctype.h>
#define CLASS_SIZE 5
 
 
struct student {
	char last_name[20];
	int student_id;
	char grade[5];
};
 
(main)
#include "cl_info.h"
 
float average(struct student group[]);
int main(void)
{
	struct student group[CLASS_SIZE];
	int k;
	int p;
	int m;
	
 
	for (k = 0; k < CLASS_SIZE; ++k)
	{
	
		printf("\nEnter name: ");
		scanf(" %s", &group[k].last_name); 
	}
	
	printf("Please enter grades for the following students:  \n\n");
	for (p = 0; p < CLASS_SIZE; ++p)
	{
		printf("\t%s\t", group[p].last_name);
			for (m = 0; m < 5; ++m)
			{
			scanf(" %c", &(group[p].grade[m]));
			
			}
			
		printf("\n\t Average for %s is %.1f\n\n", group[p].last_name, average(group));
	
	}
}
(average function)
#include "cl_info.h"
 
float average(struct student group[])
{
	float cnt = 0;
	int i =0;
	int j =0;
	float avg = 0;
	
 
	for (j = 0; j < 5; ++j)
	
	{
	switch(toupper(group[i].grade[j]))
 
					{
						case 'A':
							cnt += 4;
							break;
						case 'B':
							cnt += 3;
							break;
						case 'C':
							cnt += 2;
							break;
						case 'D':
							cnt += 1;
							break;
					}
	
	}
 
	
	avg = cnt/5;
	return avg;
}
 
(Here is with all the code in one file and everything working fine)
#include "pracx.h"
 
 
//float average(struct student group[]);
int main(void)
{
	struct student group[CLASS_SIZE];
	int k;
	int p;
	float average = 0;
	float cnt = 0;
	
	for (k = 0; k < CLASS_SIZE; ++k)
	{
		cnt = 0;
		printf("\nEnter name: ");
		scanf(" %s", &group[k].last_name); 
		printf("\nEnter 5 grades: ");
			for (p = 0; p < 5; ++p)
				
			{
				scanf(" %c", &(group[k].grade[p]));
				printf(" %c", group[k].grade[p]);
					switch(toupper(group[k].grade[p]))
 
					{
						case 'A':
							cnt += 4;
							break;
						case 'B':
							cnt += 3;
							break;
						case 'C':
							cnt += 2;
							break;
						case 'D':
							cnt += 1;
							break;
					}
			}
			average = cnt/5;
			printf("\nThe GPA for %s is %.1f\n", group[k].last_name, average);
	}
}

Open in new window

0
 
Infinity08Connect With a Mentor Commented:
Your average function should look like :

        float average(struct student *s) {
            /* your implementation here ... */
        }

ie. you calculate the average for a specific student, not the group.

You'd call it like this :

        printf("\n\t Average for %s is %.1f\n\n", group[p].last_name, average(&(group[p])));
0
 
pmo977Author Commented:
Thank you so much!  Everything works wonderfully.
0
All Courses

From novice to tech pro — start learning today.