Home > Back-end >  Exception has occured, unknown signal error when using class object again inside each function
Exception has occured, unknown signal error when using class object again inside each function

Time:11-19

I'm trying to write a C code for a course I'm enrolled in, where I keep the information of the students enrolled in the course.

  • I should be able to add a student to the classrrom in the user interface written in main , by calling the function void addNewStudent(int ID, string name, string surname), where I create my object instances, Student, and Course inside the function.

  • I should also be able to search by given ID by calling the function void showStudent(int ID) in the main, where the function uses the getStudent(ID) method of the object of the classCourse

  • I did not write all the methods, but when I try to debug this code, I got the error " Exception has occured, unknown signal error."

My questions are:

  • What is the reason of this error? How can I fix it?
  • Suppose that the user interface in the main is necessary to use as well as the functions it calls. Do I have to create a class object again inside each function as I wrote?
  • Can a more effective implementation be made in accordance with the object oriented principles I have defined above?
#include <iostream>
using namespace std;
#define MAX  10

class Student {
private:
   int ID;
   string name;
   string surname;

public:
   Student()
   {
      ID = 0;
      string name = "" ;
      string surname = "";

   }
   void setID(int ID_set);
   int getID();
   void setName(string name_set);
   string getName();
   void setSurName(string surname_set);
   string getSurName();
};

class Course {
   private:
      Student students[MAX];
      int num =0 ; // The current number of students in the course, initially 0.
      float weightQ;
      float weightHW;
      float weightF;

   public:
      Course()
      {
         students[num] = {};
         weightQ = 0.3;
         weightHW = 0.3;
         weightF = 0.4;

      }
      int getNum(); // Returns how many students are in the course
      void addNewStudent(Student new_student);
      void updateWeights(float weightQ_update, float weightHW_update, float weightF_update);
      void getStudent(int ID_given);
};

// Method declerations for the class Student

void Student :: setID(int ID_set){
   ID = ID_set;
}
int Student :: getID(){
   return ID;
}
void Student :: setName(string name_set){
   name = name_set;
}
string Student :: getName(){
   return name;
}
void Student :: setSurName(string surname_set){
   surname = surname_set;
}
string Student :: getSurName(){
   return surname;
}   

// Method declerations for the class Course
int Course :: getNum(){
   return num;
}
void Course :: addNewStudent(Student new_student){
   students[num] = new_student ;
   num = num   1;
}
void Course :: updateWeights(float weightQ_update, float weightHW_update, float weightF_update){
            weightQ = weightQ_update;
            weightHW = weightHW_update;
            weightF = weightF_update; 
}
void Course :: getStudent(int ID_given){
   for(int i = 0; i<MAX; i  ){
      if(ID_given == students[i].getID()){
         cout << "Student Name & Surname : " << students[i].getName() << " " << students[i].getSurName()<<"\n";
        
      }
   } 
}

 void addNewStudent(int ID, string name, string surname){
    Student student;
    Course ECE101;
    student.setID(ID);
    student.setName(name);
    student.setSurName(surname);
    ECE101.addNewStudent(student);
   }
 void showStudent(int ID){
    Course ECE101;
    ECE101.getStudent(ID);
 }

int main(){
   Course ECE101;
   cout << "Welcome to the ECE101 Classroom Interface"<<"\n";
   cout << "Choose your option\n";
   string option_1 = "1) Add a student ";
   string option_2 = "2) Search a student by ID";
   cout << "Enter your option: ";
   int x;
   int ID;
   string name, surname;
   cin >> x;
   if (x == 1)
      cout << "Enter the student ID ";
      cin >> ID;
      cout << endl;
      cout << "Enter the student name ";
      cin >> name;
      cout << endl;
      cout << "Enter the student surname " ;
      cin >> surname;
      addNewStudent(ID, name, surname);

   return 0;
}

CodePudding user response:

 To make the menu more interactive you could add a do while statement that would accept 3 options:

  • register
  • show data
  • exit
int main(){
   Course ECE101;
   int x;
   int ID;
   string name, surname;

   string option_1 = "1) Add a student ";
   string option_2 = "2) Search a student by ID";

   cout << "Welcome to the ECE101 Classroom Interface"<<"\n";
   cout << "Choose your option\n";
   cin >> x;

    do {
        if (x == 1) {
            cout << "Enter the student ID ";
            cin >> ID;
            cout << endl;
            cout << "Enter the student name ";
            cin >> name;
            cout << endl;
            cout << "Enter the student surname " ;
            cin >> surname;
            addNewStudent(ID, name, surname, ECE101);
        }
        else {
            cout << "Enter the student ID\n";
            cin >> ID;
            ECE101.getStudent(ID, ECE101);
        }
        cout << "Choose your option\n";
        cin >> x;
    } while(x != 3);

    return 0;
}

addnewStudent() and showStudent() methods now accepts an instance of Course as an argument to be able to add students.

void addNewStudent(int ID, string name, string surname, Course &course){
    Student student;
    student.setID(ID);
    student.setName(name);
    student.setSurName(surname);
    course.addNewStudent(student);
}

void showStudent(int ID, Course &course){
    course.getStudent(ID, course);
}

the function is modified from the same class as well.

void Course :: getStudent(int ID_given, Course &course){
   for(int i = 0; i<MAX; i  ){
      if(ID_given == students[i].getID()){
         cout << "Student Name & Surname : " << students[i].getName() << " " << students[i].getSurName()<<"\n";
        
      }
   } 
}

Demo

CodePudding user response:

Your addNewStudent function creates a new course everytime it is called. You could pass a reference to the course as a parameter into the function and call Course.addNewStudent(student). You'll want to make sure you specify it's a reference though when you define your function or you'll just create a copy of the course.

  • Related