Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

// Debug 12-2 A Student class // Extraction operator throws exceptions if an ID

ID: 3574729 • Letter: #

Question

// Debug 12-2 A Student class
// Extraction operator throws exceptions if an ID number is negative
// or more than four digits
// An exception also is thrown if the gpa is over 4.0
// The main() function declares an array of students and gets data
// If an exception is thrown ID becomes 9999 and gpa becomes 0
#include<iostream>
#include<string>
using namespace std;
class StudentException : runtime_error
{
   private:
      int idNum;
      double gpa;
   public:
      StudentException(int, double);
      int getIdNum();
      double getGpa();
};
class Student
{
   friend ostream& operator<<(ostream&, const Student&);
   friend istream& operator>>(istream&, Student&);
   private:
      int idNum;
      double gpa;
      Student(const int = 0, const double = 0.0);
      void setIdTo9s();
      void setGpaToZero();
};

Student::Student(const int id, const double gp)
{
idNum = id;
gpa = gpa;
}
void Student::setIdTo9s()
{
   idNum = 9999;
}
void Student::setGpaToZero()
{
   gpa = 0;
}

ostream& operator<<(ostream& out, const Student& stu)
{
   out << "Student ID #" << stu.idNum <<
       " gpa is " << gpa << endl;
   return out;
}
istream& operator>>(istream &in, Student &stu)
{
   const int FOUR_DIGITS = 9999;
   const double MAX_GPA = 4.0;
   cout << "Enter ID number for student ";
   in >> stu.idNum;

   if(stu.idNum < 0 || stu.idNum > FOUR_DIGITS)
   {
      StudentException se(stu.idNum, 0);
      throw(se);
   }
   cout << "Enter gpa ";
   in >> stu.gpa;
   if(stu.gpa > MAX_GPA)
   {
       StudentException se(stu.idNum, gpa);
       throw(se);
   }
   return in;
}

StudentException::StudentException(int id, double gpa) : runtime_error("Student exception")
{
    idNum = id;
    this->gpa = gpa;
}
int StudentException::getIdNum()
{
   return idNum;
}
double StudentException::getGpa()
{
   return stu.gpa;
}

int main()
{
   const int NUM = 5;
   Student stus[NUM];
   int x;
   for(x = 0; x < NUM; ++x)
   {
     try
     {
        cin >> stus[NUM];
     }
     catch(StudentException se)
     {
        cout << se.what() << endl;
        stus[x].setIdTo9s();
        stus[x].setGpaToZero();
     }
   }
   cout << endl << "Student summary:" << endl;
   for(x = 0; x < NUM; ++x)
       cout << stus[NUM] << endl;
   return 0;
}

Explanation / Answer

#include<iostream>
#include<string>
using namespace std;
class StudentException
{
   public:
      int idNum;
      double gpa;
   public:
      StudentException(int, double);
      int getIdNum();
      double getGpa();
};
class Student
{
   friend ostream& operator<<(ostream&, const Student&);
   friend istream& operator>>(istream&, Student&);
   public:
      int idNum;
      double gpa;
      Student(const int = 0, const double = 0.0);
      void setIdTo9s();
      void setGpaToZero();
};

Student::Student(const int id, const double gp)
{
idNum = id;
gpa = gpa;
}
void Student::setIdTo9s()
{
   idNum = 9999;
}
void Student::setGpaToZero()
{
   gpa = 0;
}

ostream& operator<<(ostream& out, const Student& stu)
{
   out << "Student ID #" << stu.idNum <<
       " gpa is " << stu.gpa << endl;
   return out;
}
istream& operator>>(istream &in, Student &stu)
{
   const int FOUR_DIGITS = 9999;
   const double MAX_GPA = 4.0;
   cout << "Enter ID number for student ";
   in >> stu.idNum;

   if(stu.idNum < 0 || stu.idNum > FOUR_DIGITS)
   {
      StudentException se(stu.idNum, 0);
      throw(se);
   }
   cout << "Enter gpa ";
   in >> stu.gpa;
   if(stu.gpa > MAX_GPA)
   {
       StudentException se(stu.idNum, se.gpa);
       throw(se);
   }
   return in;
}

StudentException::StudentException(int id, double gpa)
{
    idNum = id;
    this->gpa = gpa;
}
int StudentException::getIdNum()
{
   return idNum;
}
double StudentException::getGpa()
{
   return gpa;
}

int main()
{
   const int NUM = 5;
   Student stus[NUM];
   int x;
   for(x = 0; x < NUM; ++x)
   {
     try
     {
        cin >> stus[NUM];
     }
     catch(StudentException se)
     {
        cout << "excaption";
        stus[x].setIdTo9s();
        stus[x].setGpaToZero();
     }
   }
   cout << endl << "Student summary:" << endl;
   for(x = 0; x < NUM; ++x)
       cout << stus[NUM] << endl;
   return 0;
}