Hi, i'm supposed to write a function, countGrade that takes in the Grade and find the number of students that have this grade. This value is returned to the calling program. The function prototype is int countGrade(char). Currently all i have written is this.
Seem like there's something wrong with my function but i have no idea how to fix it. Any help would be appreciated. Thanks in advance!

#include <iostream>
#include <string>
using namespace std;

struct markRec {
	string id;
	string name;
	double tt, fe, score;
	char grade;
};

struct markRec myMark[] = {
	{"B001", "Tan Choon Seng", 34.0, 67.0, 53.8, 'D'},
	{"B002", "Anne Smith", 79.0, 88.2, 83.6, 'A' },
	{"B003", "Alice Lim", 56.0, 45.0, 49.4, 'F' },
	{"N003", "Alan Phang", 67.0, 87.0, 79.0, 'B'},
	{"N005", "Ho Tock Seng", 60.0, 67.0, 64.2, 'C'},
	{"N0056", "Tan Bee Hoon", 79.0, 88.1, 83.6, 'A'},

};
//The main program
int main (){
	char c;
	cout << "Enter grade : " ;
	cin >> c;

	countGrade (c);

	return 0;
}
//Function 
int countGrade (char c)

{
  char grade; 
  int count;

  
  for ( count = 0; count < grade.size; ++count ) {
	if (grade == c )
	cout << "The number of students that have grade" << c << "are" << count << endl;
  }
  
  return count;
}

Edited 6 Years Ago by grahf23: n/a

When the C++ compiler parse your code line by line.
Until it sees the line number 32 there will be no symbol name called
countGrade on the symbol table.

So you have to declare it's prototype before you use it in the main.
on line number 4 bellow.

#include <iostream>
#include <string>
using namespace std;
int countGrade (char );
struct markRec {
	string id;
	string name;
	double tt, fe, score;
	char grade;
};

struct markRec myMark[] = {
	{"B001", "Tan Choon Seng", 34.0, 67.0, 53.8, 'D'},
	{"B002", "Anne Smith", 79.0, 88.2, 83.6, 'A' },
	{"B003", "Alice Lim", 56.0, 45.0, 49.4, 'F' },
	{"N003", "Alan Phang", 67.0, 87.0, 79.0, 'B'},
	{"N005", "Ho Tock Seng", 60.0, 67.0, 64.2, 'C'},
	{"N0056", "Tan Bee Hoon", 79.0, 88.1, 83.6, 'A'},

};
//The main program
int main (){
	char c;
	cout << "Enter grade : " ;
	cin >> c;

	countGrade (c);

	return 0;
}
//Function 
int countGrade (char c)

{
  char grade; 
  int count;

  
  for ( count = 0; count < grade.size; ++count ) {
	if (grade = c )
	cout << "The number of students that have grade" << c << "are" << count << endl;
  }
  
  return count;
}

Edited 6 Years Ago by NicAx64: n/a

Thanks for the help but i'm still getting this error from my compiler..
error C2228: left of '.size' must have class/struct/union
1> type is 'char'

the error is at line 39.

for ( count = 0; count < grade.size; ++count )

You can see grade is a type `char' how it can have a member 'size'?

Edited 6 Years Ago by NicAx64: n/a

As I review your code , it should be corrected to.

int count =0;
  for ( int i = 0; i < sizeof(myMark)/sizeof(markRec); ++i ) {
	if (myMark[count].grade = c )
	 count++;
  }
  return count;

Think and understand this is important.
Review on your code.If any questions ask.

Edited 6 Years Ago by NicAx64: sorry mistake

Thanks for the help! I don't quite under this line

i < sizeof(myMark)/sizeof(markRec);

Any help?
By the way, i have tried to run the program but no matter what grade i key in, the output is alway 6. Any idea? Thanks!

Check your if statement. Your comparison is not a comparison, but an assignment. A single = is assignment (int y = b), whereas a double == is comparison ( if(x == 5) ).

Change that if statement and let us know what happens.

Also, the sizeof(myMark) / sizeof(markRec) line is figuring out the count of records in your array of markRecs.

Edited 6 Years Ago by Sodabread: n/a

Check your if statement. Your comparison is not a comparison, but an assignment. A single = is assignment (int y = b), whereas a double == is comparison ( if(x == 5) ).

Change that if statement and let us know what happens.

Also, the sizeof(myMark) / sizeof(markRec) line is figuring out the count of records in your array of markRecs.

I have tried if (myMark[count].grade == c ) and end up with no output. Any ideas?

I don't know what your input is, don't forget that a character comparison is case sensitive. If you're entering "c" from the console, it won't match a grade of "C". You can use character arithmetic based on the ASCII table to determine whether input is lower or upper case and rectify it if it's not what you want.

I don't know what your input is, don't forget that a character comparison is case sensitive. If you're entering "c" from the console, it won't match a grade of "C". You can use character arithmetic based on the ASCII table to determine whether input is lower or upper case and rectify it if it's not what you want.

The result is 0 when i use == no matter what i enter whereas the is result is always 6 when i use =.

Edited 6 Years Ago by grahf23: n/a

Well, graff23 if you have any problem still in your code, then post your latest current code
alongwith any problems.
-Manoj

I have change myMark[count] to myMark and the function is now performing as it should. Thanks for all the help!

This question has already been answered. Start a new discussion instead.