Solved

C program problem with structures and functions

Posted on 2007-11-24
6
200 Views
Last Modified: 2010-04-15
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

0
Comment
Question by:pmo977
  • 3
  • 2
6 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 75 total points
ID: 20342712
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20344821
Have you tried?
0
 

Author Comment

by:pmo977
ID: 20345322
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:pmo977
ID: 20346293
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
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 50 total points
ID: 20346304
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
 

Author Comment

by:pmo977
ID: 20348004
Thank you so much!  Everything works wonderfully.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question