Click here to Skip to main content
15,889,876 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to write a main for this class, I want the user to decide the number of students and then enter their names the number of classes and class hours, then calculate their GPA and display their information can you please help me to write it ?

What I have tried:

#ifndef RATIONAL_H_INCLUDED
#define RATIONAL_H_INCLUDED

class Rational
{
    int numerator, denominator;
public:

    Rational(int x=0, int y=1)
    {
        numerator=x;
        if(y!=0){denominator=y;}

    };


    const int getNumerator()
    {
        return numerator ;
    };


    const int getDenomerator()
    {
        return denominator ;
    };

    friend istream operator>>(istream &,Rational&o){
    o.numerator>>o.denminator;
    };

    const &Rational operator +(Rational& sec)
    {
        int num = numerator*sec.getDenomerator()+sec.getNumerator*denominator;
        int den = sec.getDenomerator*denominator;
        if(den == 0)
        {
            Rational r;
            return r;
        }

        Rational r(num,den);
        return & r;
    }
    const &Rational operator -(Rational& sec)
    {
        int num = numerator*sec.getDenomerator() - sec.getNumerator*denominator;
        int den = sec.getDenomerator*denominator;
        if(den == 0)
        {
            Rational r;
            return r;
        }

        Rational r(num,den);
        return & r;
    }
    const &Rational operator *(Rational& sec)
    {
        int num = numerator*sec.getNumerator;
        int den = sec.getDenomerator*denominator;
        if(den == 0)
        {
            Rational r;
            return r;
        }

        Rational r(num,den);
        return & r;
    }
    const int compare (Rational& sec)
    {
        double seco= sec.getNumerator / sec.getDenomerator();
        double first =numerator / denominator;
        if(first>seco)
        {
            return 1;
        }
        else if (seco == first)
        {
            return 0;
        }
        else if (first< seco)
        {
            return -1 ;
        }
    }
    const bool operator ==(Rational& sec)
    {
        double seco= sec.getNumerator / sec.getDenomerator();
        double first =numerator / denominator;
        if(first== seco)
        {
            return true;
        }
        else
            return false ;

    }
    const int intValue()
    {
        return numerator/denominator;
    }

    const double  doubleValue()
    {
        return numerator/denominator;
    }
    const string to_string()
    {
        return to_string(numerator/denominator);
    }
    int gcd (int n, int d )
    {
        int gcd ;
        for (int i = 1; i <= n && i <= d; i++)
        {
            if (n % i == 0 && d % i == 0)
            {
                gcd = i;
            }
        }
        return gcd ;
    }





    virtual ~Rational();

protected:

private:
};

#endif // RATIONAL_H_INCLUDED


#ifndef STUDENT_H_INCLUDED
#define STUDENT_H_INCLUDED
#include "Rational.h"
#include <string>
#include <iostream>
using namespace std;
class Rational;

class Student {
    Rational * grades;
    int *cridetHours;
    int arraySize;
    string name,id;

public:
    student(){
        grades = new Rational [arraySize];
        cridetHours = new int [arraySize];
    }
    Rational Student ::calcGpa(){
 double n=0;
 double d=0;
 for (int i=0;i<arraySize;i++){
    friend istream &operator >>(istream &is, student &o){
    o.name>>o.id>>o.grades[i]>>o.cridetHours[i];}
    n=n+(grades[i].doubleValue()*cridetHours[i]);
    d=d+cridetHours[i];
 }
 Rational r(n,d);
 return r;


};
     ostream &operator <<(ostream &output, student &s);
     istream &operator>>(istream &input ,student &s);
};
#endif // STUDENT_H_INCLUDED


int main()
{
    string name;
    int vc,id,subject;
    cout<<"Enter the number of student:";
    cin >> vc;
    for(int i=0;i<vc;i++){
        cout<<"Enter the name:";
        cin>>name;
        cout<<"enter the id:";
        cin>>id;
        cout<<"enter the number sublect:";
        cin>>subject;
        for(int i=0;i<subject;i++)
            cout>>"enter the grade of subject";
    }
}
Posted
Updated 9-May-22 20:17pm

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:
C++
int main()
{
   while(add_student());  // add students until no more are entered
   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.
C++
#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;  // global array of students

bool add_student()
{
   Student student;
   // get name
   if(name == "#") return false;
   // set name
   // add grades
   students.push_back(student);
   return true;
}

void calculate_gpas()
{
   for(auto s = students.cbegin(); s != students.cend(); ++s)
   {
      s->calculate_gpa();
   }
}

void display_students()
{
   // a lot like calculate_gpas
}
 
Share this answer
 
Comments
0x01AA 9-May-22 16:56pm    
Robust, 5.
Is is not clear why should you use the Rational class. In any case it is syntactically and semantically broken. If you really need rational numbers, use std::ratio instead. You might also consider using std::vector instead of dynamically allocating memory yourself.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900