I will sketch out some code, and you'll probably have to fill in the details before anyone else will help you further.
Based on your requirements, I have no idea why you need the
Rational
class. It's as if you've just found some code and dumped it together. And your code prompts for class grades, which makes more sense than the the class hours stated in the requirements.
A
main
should always be simple. Something like this would do:
int main()
{
while(add_student()); calculate_gpas();
display_students();
}
Having to enter the number of students first, and then
forcing that many to be entered, isn't user friendly. Imagine someone having to count hundreds of students just so they can enter that number. Instead, entering a name like
"#"
can be used to indicate that no more students will be entered. The same is true for the number of grades. You should define an illegal grade which indicates that all the grades have been entered.
Now you need the three functions invoked by
main
and a
Student
class. I'm leaving the implementation of the functions declared in the
Student
class up to you. Note that I've used
vector[
^], which is far better than messing about with arrays and
new
. After
string
, it's the next thing in
std
that you should learn how to use.
#include "iostream"
#include "string"
#include "vector"
using namespace std;
class Student
{
Student();
void set_name(string& name);
void add_grade(unsigned int grade);
void calculate_gpa();
void display() const;
string name_;
vector<unsigned int> grades_;
double gpa_;
};
vector<Student> students;
bool add_student()
{
Student student;
if(name == "#") return false;
students.push_back(student);
return true;
}
void calculate_gpas()
{
for(auto s = students.cbegin(); s != students.cend(); ++s)
{
s->calculate_gpa();
}
}
void display_students()
{
}