Question

In: Computer Science

C++, Write an Exception Class for the Student class you created in the initial project you...

C++, Write an Exception Class for the Student class you created in the initial project you worked on.  Create a try catch block that would catch negative numbers for the Student id.

// header file

#pragma once
#include<iostream>
#include<string>
constexpr auto MAX = 100;
using namespace std;

class Roster1
{

private:
   string student_name;

   int student_id;

   int final_grade;

   string letter_grade;
public:
   //constroctor
   Roster1();

   //destroctor
   ~Roster1();

   // setters
   void setStudents(string newname, int newgade, int newid);
   void validatename(string newname);
   void setstudent_name(string newname);
   void setstudent_id(int newid);
   void setfinal_grade(int newgrade);
   void setletter_grade(int newgrade);
   void validateGrade(int newgrade);
   int validateID(int newid);
   //getters
   string getstudent_name();
   int getfinal_grade();
   string getletter_grade();
   int getstudent_id();
   //int getStudents();
   //void getData();


};
// Class definition (implementation)

#include "Roster1.h"


Roster1::Roster1()
{
   student_name = "";

   student_id = 0;

   final_grade = 0;
}


Roster1::~Roster1()
{
}

void Roster1::validateGrade(int newgrade) {

   final_grade = newgrade;

   if (final_grade >= 0 && final_grade <= 100);


   else

       cout << "Enter a Valid grade.";

}

//int Roster1::validateID(int newid) {
  
  
  
  

//}
void Roster1::validatename(string newname) {
   student_name = newname;
   bool beta = false;
   int nameLength = student_name.length();
   for (int i = 0;i < nameLength;i++)
   {
       if (isalpha(student_name[i]))
       {
           cout << student_name[i] << " is a letter.\n";
       }
       else
       {
           cout << "First instance of a non char is at index "
               << student_name.find_first_not_of("abcdefghijklmnopqrstuvwxyz", 0) << ".\n";
           beta = true;
           if (beta == true)
           {
               cout << "Enter a name with characters only.\n";
               cin >> student_name;
           }
       }
   }

}

void Roster1::setStudents(string newname, int newgrade, int newid)
{
   student_name = newname;

   student_id = newid;

   final_grade = newgrade;

}


void Roster1::setstudent_name(string newname) {
   student_name = newname;
}
void Roster1::setstudent_id(int newid) {

   student_id = newid;

}
void Roster1::setfinal_grade(int newgrade) {
   final_grade = newgrade;

}
void Roster1::setletter_grade(int newgrade) {
   if (final_grade >= 93 && final_grade <= 100)

       letter_grade = "A";

   else if (final_grade >= 90 && final_grade <= 92)

       letter_grade = "A-";

   else if (final_grade >= 87 && final_grade <= 89)

       letter_grade = "B+";

   else if (final_grade >= 83 && final_grade <= 86)

       letter_grade = "B";

   else if (final_grade >= 80 && final_grade <= 82)

       letter_grade = "B-";

   else if (final_grade >= 77 && final_grade <= 79)

       letter_grade = "C+";

   else if (final_grade >= 73 && final_grade <= 76)

       letter_grade = "C";

   else if (final_grade >= 70 && final_grade <= 72)

       letter_grade = "C-";

   else if (final_grade >= 67 && final_grade <= 69)

       letter_grade = "D+";

   else if (final_grade >= 60 && final_grade <= 66)

       letter_grade = "D";

   else

       letter_grade = "F";
}


string Roster1::getstudent_name() {
   return student_name;
}
int Roster1::getfinal_grade() {
   return final_grade;
}
string Roster1::getletter_grade() {
   return letter_grade;
}
int Roster1::getstudent_id() {
   return student_id;
}

// main

#include<iostream>
#include<string>
#include "roster1.h"
using namespace std;

int menu() {

   int c;

   while (true)

   {
       // created a menu outside of the main, it returns the value that the user input to the main

       cout << "This program will record a student record" << endl;
       cout << "1). Add the Student Record." << endl;

       cout << "2). Delete Student Record." << endl;

       cout << "3). Display Student Record." << endl;
       cout << "4). Display average grade" << endl;

       cout << "5). Exit" << endl;

       cout << "Please enter the choice (1 to 5):";

       cin >> c;

       if (c >= 1 && c <= 5)

           return c;

       else
           continue;


   }

}
int main()
{
   Roster1 std[100]; // array

   int choice, student_count = 0, flag = 0;

   string name;
   int id, grade, sum = 0;
   double average = 0;

   bool con = true;

   while (con) // while loop

   {


       choice = menu(); // brings the value from int menu and declare it to choice
       switch (choice)

       {

       case 1:

       cout << "Enter Student name:";
       cin >> name;
       std[student_count].validatename(name);

          
       cout << "Enter Student ID:";
                  
       cin >> id;
                  
                  
       cout << "Enter the Grade:";
       cin >> grade;
       std[student_count].validateGrade(grade);
       std[student_count].setletter_grade(grade);
       std[student_count].setStudents(name, grade, id);
       student_count++;
       cout << "Student " << student_count + 0 << " Stored Successfully " << endl; // display to the user the amount of student so far

       break;

       case 2:

           cout << "Enter the student id you want to delete:";

           cin >> id;

           for (int i = 0;i < student_count;i++)

           {

               if (std[i].getstudent_id() == id)

               {

                   //std[i].~student();

                   flag = 1;

                   cout << "Student Record deleted;" << endl;

                   break;

               }

           }

           if (flag == 0)

           {

               cout << "Student not found!" << endl;

           }

           break;

       case 3:

           cout << "Enter the student id you want display record:";

           cin >> id;

           flag = 0;

           for (int i = 0;i < student_count; i++)

           {

               if (std[i].getstudent_id() == id)

               {

                   cout << "Student name\t:" << std[i].getstudent_name() << endl;

                   cout << "Student id\t:" << std[i].getstudent_id() << endl;

                   cout << "Student final grade\t:" << std[i].getfinal_grade() << endl;

                   cout << "Student Letter Grade\t:" << std[i].getletter_grade() << endl;

                   flag = 1;

                   break;

               }

           }

           if (flag == 0)

           {

               cout << "Student not found!" << endl;

           }

           break;

       case 4:
           average = 0;
           sum = 0;


           for (int i = 0; i < student_count;i++) {
               sum = sum + std[i].getfinal_grade();
           }
           average = ((double)sum) / student_count;
           cout << "average grade of the class is: " << average << endl;
           break;


       case 5:

           con = false; // makes con false to finish the program
           break;

   }


   }

   return 0;

Solutions

Expert Solution

Please find the updated code below::

Roster1.h


#ifndef ROSTER1_HPP_
#define ROSTER1_HPP_

// header file

#pragma once
#include<iostream>
#include<string>
const auto MAX = 100;
using namespace std;

class Roster1
{

private:
string student_name;

int student_id;

int final_grade;

string letter_grade;
public:
//constroctor
Roster1();

//destroctor
~Roster1();

// setters
void setStudents(string newname, int newgade, int newid);
void validatename(string newname);
void setstudent_name(string newname);
void setstudent_id(int newid);
void setfinal_grade(int newgrade);
void setletter_grade(int newgrade);
void validateGrade(int newgrade);
int validateID(int newid);
//getters
string getstudent_name();
int getfinal_grade();
string getletter_grade();
int getstudent_id();
//int getStudents();
//void getData();


};
// Class definition (implementation)

#endif /* ROSTER1_HPP_ */

Roster1.cpp

#include "Roster1.h"


Roster1::Roster1()
{
student_name = "";

student_id = 0;

final_grade = 0;
}


Roster1::~Roster1()
{
}

void Roster1::validateGrade(int newgrade) {

final_grade = newgrade;

if (final_grade >= 0 && final_grade <= 100);


else

cout << "Enter a Valid grade.";

}

//int Roster1::validateID(int newid) {

//}
void Roster1::validatename(string newname) {
student_name = newname;
bool beta = false;
int nameLength = student_name.length();
for (int i = 0;i < nameLength;i++)
{
if (isalpha(student_name[i]))
{
cout << student_name[i] << " is a letter.\n";
}
else
{
cout << "First instance of a non char is at index "
<< student_name.find_first_not_of("abcdefghijklmnopqrstuvwxyz", 0) << ".\n";
beta = true;
if (beta == true)
{
cout << "Enter a name with characters only.\n";
cin >> student_name;
}
}
}

}

void Roster1::setStudents(string newname, int newgrade, int newid)
{
student_name = newname;

student_id = newid;

final_grade = newgrade;

}


void Roster1::setstudent_name(string newname) {
student_name = newname;
}
void Roster1::setstudent_id(int newid) {

student_id = newid;

}
void Roster1::setfinal_grade(int newgrade) {
final_grade = newgrade;

}
void Roster1::setletter_grade(int newgrade) {
if (final_grade >= 93 && final_grade <= 100)

letter_grade = "A";

else if (final_grade >= 90 && final_grade <= 92)

letter_grade = "A-";

else if (final_grade >= 87 && final_grade <= 89)

letter_grade = "B+";

else if (final_grade >= 83 && final_grade <= 86)

letter_grade = "B";

else if (final_grade >= 80 && final_grade <= 82)

letter_grade = "B-";

else if (final_grade >= 77 && final_grade <= 79)

letter_grade = "C+";

else if (final_grade >= 73 && final_grade <= 76)

letter_grade = "C";

else if (final_grade >= 70 && final_grade <= 72)

letter_grade = "C-";

else if (final_grade >= 67 && final_grade <= 69)

letter_grade = "D+";

else if (final_grade >= 60 && final_grade <= 66)

letter_grade = "D";

else

letter_grade = "F";
}


string Roster1::getstudent_name() {
return student_name;
}
int Roster1::getfinal_grade() {
return final_grade;
}
string Roster1::getletter_grade() {
return letter_grade;
}
int Roster1::getstudent_id() {
return student_id;
}

main.cpp

#include<iostream>
#include<string>
#include "roster1.h"
using namespace std;


class NegativeID: public exception
{
   virtual const char* what() const throw()
                               {
       return "Negative Student number entered!!!";
                               }
}NegativeIDExp;

int menu() {

   int c;

   while (true)

   {
       // created a menu outside of the main, it returns the value that the user input to the main

       cout << "This program will record a student record" << endl;
       cout << "1). Add the Student Record." << endl;

       cout << "2). Delete Student Record." << endl;

       cout << "3). Display Student Record." << endl;
       cout << "4). Display average grade" << endl;

       cout << "5). Exit" << endl;

       cout << "Please enter the choice (1 to 5):";

       cin >> c;

       if (c >= 1 && c <= 5)

           return c;

       else
           continue;


   }

}
int main()
{
   Roster1 std[100]; // array

   int choice, student_count = 0, flag = 0;

   string name;
   int id, grade, sum = 0;
   double average = 0;

   bool con = true;

   while (con) // while loop

   {

       try{

           choice = menu(); // brings the value from int menu and declare it to choice
           switch (choice)

           {

           case 1:

               cout << "Enter Student name:";
               cin >> name;
               std[student_count].validatename(name);


               cout << "Enter Student ID:";

               cin >> id;

               if(id<0){
                   throw NegativeIDExp;
               }

               cout << "Enter the Grade:";
               cin >> grade;
               std[student_count].validateGrade(grade);
               std[student_count].setletter_grade(grade);
               std[student_count].setStudents(name, grade, id);
               student_count++;
               cout << "Student " << student_count + 0 << " Stored Successfully " << endl; // display to the user the amount of student so far

               break;

           case 2:

               cout << "Enter the student id you want to delete:";

               cin >> id;

               if(id<0){
                   throw NegativeIDExp;
               }

               for (int i = 0;i < student_count;i++)

               {

                   if (std[i].getstudent_id() == id)

                   {

                       //std[i].~student();

                       flag = 1;

                       cout << "Student Record deleted;" << endl;

                       break;

                   }

               }

               if (flag == 0)

               {

                   cout << "Student not found!" << endl;

               }

               break;

           case 3:

               cout << "Enter the student id you want display record:";

               cin >> id;

               if(id<0){
                   throw NegativeIDExp;
               }

               flag = 0;

               for (int i = 0;i < student_count; i++)

               {

                   if (std[i].getstudent_id() == id)

                   {

                       cout << "Student name\t:" << std[i].getstudent_name() << endl;

                       cout << "Student id\t:" << std[i].getstudent_id() << endl;

                       cout << "Student final grade\t:" << std[i].getfinal_grade() << endl;

                       cout << "Student Letter Grade\t:" << std[i].getletter_grade() << endl;

                       flag = 1;

                       break;

                   }

               }

               if (flag == 0)

               {

                   cout << "Student not found!" << endl;

               }

               break;

           case 4:
               average = 0;
               sum = 0;


               for (int i = 0; i < student_count;i++) {
                   sum = sum + std[i].getfinal_grade();
               }
               average = ((double)sum) / student_count;
               cout << "average grade of the class is: " << average << endl;
               break;


           case 5:

               con = false; // makes con false to finish the program
               break;

           }


       }catch (exception& e) {
           cout << "Error!!!"<<e.what() << "\n\n\n";
       }
   }

   return 0;

}

output:


Related Solutions

To be Completed in C# Write a program that creates an exception class called ManyCharactersException, designed...
To be Completed in C# Write a program that creates an exception class called ManyCharactersException, designed to be thrown when a string is discovered that has too many characters in it. Consider a program that takes in the last names of users. The driver class of the program reads the names (strings) from the user until the user enters "DONE". If a name is entered that has more than 20 characters, throw the exception. Design the program such that it...
1) Design an implement a program that created and exception class called StringTooLongException, designed to be...
1) Design an implement a program that created and exception class called StringTooLongException, designed to be thrown when a string is discovered that has too many characters in it. Create a driver that reads in strings from the user until the user enters DONE. If a string that has more then 5 characters is entered, throw the exception. Allow the thrown exception to terminate the program 2) Create a class called TestScore that has a constructor that accepts an array...
Write a C program that calculates a student grade in the C Programming Class. Ask the...
Write a C program that calculates a student grade in the C Programming Class. Ask the user to enter the grades for each one of the assignments completed in class: Quiz #1 - 25 points Quiz #2 - 50 points Quiz #3 - 30 points Project #1 - 100 points Project #2 - 100 points Final Test - 100 points The total of the quizzes count for a 30% of the total grade, the total of the projects counts for...
Java Modify subclass HourlyEmployee11 and class HourlyExc (created to hold the exception) to add a "try...
Java Modify subclass HourlyEmployee11 and class HourlyExc (created to hold the exception) to add a "try and catch" statement to catch an exception if "empStatus == 1" hourly wage is not between $15.00/hr. and $25.00/hr. The keywords “throw” and “throws” MUST be used correctly and both keywords can be used either in a constructor or in a method. If an exception is thrown, the program code should prompt the user for the valid input and read it in. *NOTE*- I...
********************C# C# C#******************** Part A: Create a project with a Program class and write the following...
********************C# C# C#******************** Part A: Create a project with a Program class and write the following two methods(headers provided) as described below: 1. A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number if the number is not in the range...
Write a C program that prints the Grade of each student in a class based on...
Write a C program that prints the Grade of each student in a class based on their mark. The program must also print the average mark of the class. The program must prompt (ask) the user for the mark of a student (out of 100). The program must then print the mark entered and the grade received based on the table below. Grade Range A 85 to 100 inclusive B 75 to 85 inclusive C 60 to 70 inclusive D...
c++ Add exception handling to the MyStack class (e.g. an instance of the class should throw...
c++ Add exception handling to the MyStack class (e.g. an instance of the class should throw an exception if an attempt is made to remove a value from an empty stack) and use the MyStack class to measure the execution cost of throwing an exception. Create an empty stack and, within a loop, repeatedly execute the following try block: try { i n t s t a c k . pop ( ) ; } catch ( e x c...
EXCEPTION HANDLING Concept Summary: 1. Exception handling 2. Class design Description Write a program that creates...
EXCEPTION HANDLING Concept Summary: 1. Exception handling 2. Class design Description Write a program that creates an exception class called ManyCharactersException, designed to be thrown when a string is discovered that has too many characters in it. Consider a program that takes in the last names of users. The driver class of the program reads the names (strings) from the user until the user enters "DONE". If a name is entered that has more than 20 characters, throw the exception....
*In Java please! EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a  ...
*In Java please! EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a   program   that   creates   an   exception   class   called   ManyCharactersException,   designed   to   be   thrown   when   a   string   is   discovered   that   has   too   many   characters   in   it. Consider   a   program   that   takes   in   the   last   names   of   users.   The   driver   class   of   the   program reads the   names   (strings) from   the   user   until   the   user   enters   "DONE".   If   a   name is   entered   that   has   more   than   20   characters,  ...
EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a   program   that   creates  ...
EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a   program   that   creates   an   exception   class   called   ManyCharactersException,   designed   to be thrown when a   string is discovered that has too many characters in it. Consider a   program that   takes   in the last   names   of   users.   The   driver   class   of   the   program reads the   names   (strings) from   the   user   until   the   user   enters   "DONE".   If   a   name is   entered   that   has   more   than   20   characters,   throw   the   exception.  ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT