How to calculate the grading policy with struct?

I am doing a lab practice and I'm stucked in some part... below is my question

Write a grading program for a class with the following grading policies:

a. There are two quizzes, each graded on the basis of 10 points.
b. There is one midterm exam and one final exam, each graded on the basis of 100 points.
c. The final exam counts for 50% of the grade, a midterm counts for 25% and the two quizzes together count for a total of 25%.

Any grade of 90 or more is an A, any grade of 80 or more (but less than 90) is a B, any grade of 70 or more (but less than 80) is a C, any grade of 60 or more (but less than 70) is a D, and any grade below 60 is an F.

The program will read in the student’s score and output the student’s record, which consists of two quiz and two exam scores as well as the student’s average numeric score for the entire course and the final letter grade. Define and use structure for the student record.


So far my code is like this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using namespace std;

struct student_result{
       int quiz1,quiz2;       int midterm;
       int final;
       double total_mark;

};

int main()
{
     struct student_result result;
     cout<< "Enter first quiz mark: " << result.quiz1<<endl;
     cin>>result.quiz1;
     cout<< "Enter second quiz mark: "<< result.quiz2<<endl;
     cin>>result.quiz2;
     cout<< "Enter midterm exam mark: "<< result.midterm<<endl;
     cin>>result.midterm;
     cout<< "Enter final exam mark: " << result.final<<endl;
     cin>>result.final;
     system ("pause");


     }


i know how to write the calculate function, but i duno exactly how it could be work with struct...and should I place the student input part under a function call void getResult() instead of under the main program or what? somore I get weird numbers when the program output display the line " Enter first quiz mark:(this is where the weird number is) " ...can anyone solve my problems?
I have been trying it, this is the code below :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>

using namespace std;

struct student_record{
      
       int quiz1,quiz2;
       double midterm ;
       double final ;
       double average_quiz;
       double numeric_score;

};

int main()
{
     struct student_record result;
     cout<< "Enter first quiz mark: " << result.quiz1<<endl;
     cin>>result.quiz1;
     cout<< "Enter second quiz mark: "<< result.quiz2<<endl;
     cin>>result.quiz2;
     cout<< "Enter midterm exam mark: "<< result.midterm<<endl;
     cin>>result.midterm;
     cout<< "Enter final exam mark: " << result.final<<endl;
     cin>>result.final;
     system ("pause");


     }
     
void calculateResult(student_record& result)
{
     double quiz1_percent, quiz2_percent;
     quiz1_percent = (100*result.quiz1)/10.0;
     quiz2_percent = (100*result.quiz2)/10.0;
     double average_quiz = (quiz1_percent + quiz2_percent)/2;
     result.numeric_score = average_quiz + (result.midterm * 0.25) + (result.final * 0.50);    
     
     }
     
void displayResult(student_record& result)
{
     cout<<" Total quiz marks : " << result.average_quiz<<endl;
     cout<<" Midterm : " << result.midterm<<endl;
     cout<<" Final Exam : " << result.final<<endl;
     cout<<" Course average score : " << result.numeric_score<<endl;
     cout<<endl;
     }
     
     
     


although it have no error, but it just run the main program and doesnt calculate the average score, and i keep on getting weird number in the input part...
I don't see anywhere in main() where you call the 'displayResult()' or 'calculateResult()' functions? Also, main() should return an int when done.
Last edited on
it's is because when i try put the statement lik this:
cout<<" Course average score : " << result.calculateResult()<<endl;

it gives an error : "struct student_record has no member named 'calculateResult'

although i declare the function like this double calculateResult
it stil giv the same error because it require me to declare the function name in the struc student_record
That's because you are trying to call the function as though it is a member of student_record, which by your code it is not. You need to call the function and pass your student_record struct as a parameter of the function.
could u show me the examples? few statement wil do because i need some ideas to invoke :)
Ok, let's take a look at how you declared your functions:

1
2
void calculateResult(student_record&);
void displayResult(student_record&);


This means that these functions each: a) return nothing b) require a parameter of the type 'student_record' reference. So, when you want to use the function, that is exactly how you must call it. Take this example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// class definition
class Base
{
public:
  int x;
};

// global function prototype
void doSomething(Base&);


// main function, program entry point
int main()
{
  Base b;

  b.x = 0;

  doSomething(b);  // LOOK AT THIS LINE OF CODE

  return 0;
}

// global function implementation
void doSomething(Base& obj)
{
  obj.x = 1;
}


Now, for the purposes of this explanation, you can think my class as the same as your struct. I declare a function 'doSomething(Base&)' with no return type and taking a reference of class 'Base'. This is the same thing you are doing. You must call your functions exactly as I have called mine. You need to pass your 'student_result' variable to your functions.
Topic archived. No new replies allowed.