I am trying to teach myself C++ and using Problem Solving in C++ by Walter Savitch. This is a program project from the book that I used to practice using classes/ structs and would like some feedback, please. I included in the notes the question as well as tried to specify my functions for others to try to see my train of thought on each.
It's a work in progress and as I said, I'm still learning so I know I can improve certain things or make this more efficient. Some things I noted for myself:
- In
main
, to get each percentage I passed as parameters 20 or 100 then 25 or 50 explicitly there and thought I should make use of global variables to make it more applicable for general use and if those percentages may want to be changed easier, but I did this for now because my focus is practicing using classes/ structs. - along with that, I explicitly made the quiz array to hold 2 values (quiz[2]) and am considering making that a dynamic array by which I can ask the user for how many quiz grades there are (but I'll admit, I'm still working on practicing with pointers as well so Like I said, I made this to facilitate my ease of primarily focusing on uses of classes/ structs)
- to convert the numeric grade to a letter grade, I considered using a
switch
statement, but opted to useif
,else if
,else
. I could add something in case the numeric grades are out of range when the user inputs it, but left it like this for now as I am inputting valid data when I test it myself. - the question states to define and use a structure for the student record, but as I got going I had been using the
StudentRecord
class and so made the structStudent
to be used within theStudentRecord
class. I modified it a bit, I guess, but would appreciate alternative ways of going about this.
/*
1. 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 percent of the grade, the midterm counts for
25 percent,
and the two quizzes together count for a total of 25 percent.
(Do not forget to normalize the quiz scores. They should be converted to a
percent before they are averaged in.)
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 scores 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 a structure for the student record.
*/
#include"stdafx.h"
#include<iostream>
#include<string>
using namespace std;
struct Student
{
string name;
char finalLetterGrade;
};
class StudentRecord
{
private:
Student someStudent;
double quiz[2], midterm, finalExam;
double finalGrade;
char finalLetterGrade;
public:
void inputQuizzes();
void inputMidtermGrade();
void inputFinalGrade();
double *getQuizzes();
double getMidterm();
double getFinalExam();
double calcPercent(double grade, double outOfTotalPts, double
percentOfTotal);
// general application for all scores //
double setFinalNumericGrade(double newFinalGrade);
char setFinalLetterGrade(char newFinalLetterGrade);
char calcFinalLetterGrade(double finalGrade);
};
// returns ptr to first quiz grade of array //
double *StudentRecord::getQuizzes()
{
double *quizPtr;
quizPtr = quiz;
return quizPtr;
}
double StudentRecord::getMidterm() { return midterm; }
double StudentRecord::getFinalExam() { return finalExam; }
// calculates percent of total final numeric grade //
// parameters : grade recieved (numerator), //
// max pts could received based on type of test/quiz(denominator), //
// percent of final numeric grade (25, 50) //
double StudentRecord::calcPercent(double grade, double outOfTotalPts, double
percentOfTotal)
{
double totalPercent = (grade / outOfTotalPts) * percentOfTotal;
return totalPercent;
}
///////////////////////////////////////////////////////////////////
double StudentRecord::setFinalNumericGrade(double newFinalGrade) { return
finalGrade = newFinalGrade; }
// takes final numeric grade as parameter //
// returns letter grade as char //
char StudentRecord::calcFinalLetterGrade(double finalGrade)
{
/*
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.
*/
if (finalGrade >= 90)
return 'A';
else if (finalGrade >= 80 && finalGrade < 90)
return 'B';
else if (finalGrade >= 70 && finalGrade < 80)
return 'C';
else if (finalGrade >= 60 && finalGrade < 70)
return 'D';
else
return 'F';
}
///////////////////////////////////////////////////////////////////////////
char StudentRecord::setFinalLetterGrade(char newFinalLetterGrade) { return
finalLetterGrade = newFinalLetterGrade; }
// user inputs grades //
void StudentRecord::inputQuizzes()
{
cout << "Enter quiz grades : ";
for (int i = 0; i < 2; i++)
{
cin >> quiz[i];
}
}
void StudentRecord::inputMidtermGrade()
{
cout << "Enter midterm grade : ";
cin >> midterm;
}
void StudentRecord::inputFinalGrade()
{
cout << "Enter final grade : ";
cin >> finalExam;
}
/////////////////////////////////////////////////////////////////////////
int main()
{
Student someStudent;
cout << "Enter name : ";
cin >> someStudent.name;
StudentRecord student;
student.inputQuizzes();
student.inputMidtermGrade();
student.inputFinalGrade();
double *ptr;
ptr = student.getQuizzes();
for (int i = 0; i < 2; i++)
cout << "Quiz "<< i+1 <<": "<< ptr[i] << endl;
cout << "Midterm : " << student.getMidterm() << endl;
cout << "Final Exam : " << student.getFinalExam() << endl;
// calculations //
double quizSum = 0;
for (int i = 0; i < 2; i++)
quizSum += ptr[i];
double quizPercent = student.calcPercent(quizSum, 20, 25);
double midtermPercent = student.calcPercent(student.getMidterm(), 100,
25);
double finalPercent = student.calcPercent(student.getFinalExam(), 100,
50);
double finalNumGrade = quizPercent + midtermPercent + finalPercent;
student.setFinalNumericGrade(finalNumGrade);
char letterGrade = student.calcFinalLetterGrade(finalNumGrade);
student.setFinalLetterGrade(letterGrade);
someStudent.finalLetterGrade = letterGrade;
cout << "Name : " << someStudent.name << endl;
cout << "Final Grade : " << someStudent.finalLetterGrade << endl;
return 0;
}