dimanche 6 juin 2021

Why are my if statements not working properly?

I am trying to implement the max heap data structure for record keeping. The application's code seems fine, but on running, if I select option '1' (insert a record) second time, the program gets stuck. It works fine while debugging up till 3 turns.

Also, according to the code, when I insert a wrong option, it should display the menu and ask me for the value again, but the program doesn't intake any value, instead it goes into loop and begins displaying the menu infinitely.

Code description: First is the Student class which is used in StudentMaxHeap, after that is the main() function which displays the menu.

I have kept the entire code because I am not sure what is causing this erroneous run time error.

#include<iostream>
#include<iomanip>
#include<string>
#include<cmath>
using namespace std;

class Student
{
    friend class StudentMaxHeap;
public:
    string name;
    double cgpa;
    int rollNumber;

    Student(string n = "", double c = 0, int r = -1)
    {
        name = n;
        cgpa = c;
        rollNumber = r;
    }
};
class StudentMaxHeap
{
    Student* arr; //Array of students which, arranged like a Max Heap
    int curSize; //Current number of students present in the heap
    int maxSize; //Maximum number of students that can be present in heap
public:
    StudentMaxHeap(int size) //Constructor
    {
        maxSize = size;
        arr = new Student[maxSize];
        curSize = 0;
    }
    ~StudentMaxHeap() //Destructor
    {
        delete[]arr;
        arr = 0;
    }
    bool isEmpty() //Checks whether the heap is empty or not
    {
        if (curSize == 0)
            return true;
        return false;
    }
    bool isFull() //Checks whether the heap is full or not
    {
        if (curSize == maxSize)
            return true;
        return false;
    }
    StudentMaxHeap& operator = (StudentMaxHeap& s) //Overloaded assignment operator
    {
        maxSize = s.maxSize;
        arr = new Student[maxSize];
        for (int i = 0; i < s.curSize; i++)
        {
            arr[i].name = s.arr[i].name;
            arr[i].cgpa = s.arr[i].cgpa;
            arr[i].rollNumber = s.arr[i].rollNumber;
        }
        return *this;
    }
    int parent(int i) 
    { 
        return (i - 1) / 2; 
    }
    int leftChild(int i) 
    { 
        return (2 * i + 1); 
    }
    int rightChild(int i) 
    { 
        return (2 * i + 2); 
    }
    void swap(Student s1, Student s2)
    {
        Student temp;
        temp.name = s1.name;
        temp.cgpa = s1.cgpa;
        temp.rollNumber = s1.rollNumber;
        s1.name = s2.name;
        s1.cgpa = s2.cgpa;
        s1.rollNumber = s2.rollNumber;
        s2.name = s1.name;
        s2.cgpa = s1.cgpa;
        s2.rollNumber = s1.rollNumber;      
    }
    bool insert(int rollNo, string name, double cgpa)
    {
        if (isFull())
        {
            cout << "\nError: Maximum capacity reached, cannot enter student details.\n";
            return false;
        }

        curSize++;
        int i = curSize - 1;
        arr[i] = { name, cgpa, rollNo };

        while (i != 0 && arr[parent(i)].cgpa <= arr[i].cgpa )
        {
            if (arr[parent(i)].cgpa == arr[i].cgpa)
            {
                if (arr[parent(i)].rollNumber < arr[i].rollNumber)
                {
                    swap(arr[i], arr[parent(i)]);
                    i = parent(i);
                }
            }
            else
            {
                swap(arr[i], arr[parent(i)]);
                i = parent(i);
            }
        }
    }
    Student extractTopper()
    {
        if (curSize == 0)
            exit(0);
        if (curSize == 1)
        {
            curSize--;
            return arr[0];
        }
        Student root = arr[0];
        arr[0] = arr[curSize - 1];
        curSize--;
        maxHeapify(0);

        return root;
    }
    void maxHeapify(int i)
    {
        int l = leftChild(i);
        int r = rightChild(i);
        int largest = i;
        if (l < curSize && arr[l].cgpa >= arr[i].cgpa)
        {
            if (arr[l].cgpa == arr[i].cgpa)
            {
                if (arr[l].rollNumber > arr[i].rollNumber)
                {
                    largest = l;
                }
            }
            else
                largest = l;
        }
        if (r < curSize && arr[r].cgpa > arr[largest].cgpa)
        {
            if (arr[r].cgpa == arr[i].cgpa)
            {
                if (arr[r].rollNumber > arr[i].rollNumber)
                {
                    largest = r;
                }
            }
            else
                largest = r;
        }
        if (largest != i)
        {
            swap(arr[i], arr[largest]);
            maxHeapify(largest);
        }
    }
    void displayStudentList()
    {
        cout << left << setw(20) << "Name" << setw(8) << "CGPA" << setw(14) << "Roll Number";
        for (int i = 0; i < curSize; i++)
            cout << endl<< setw(20) << arr[i].name << setw(8) << setprecision(2) << fixed << arr[i].cgpa << setw(14) << arr[i].rollNumber;
    }
    void printHeap()
    {
        int j = 0;
        for (int i = 0; i < curSize; i++)
        {
            cout << arr[i].name << " | " << setprecision(2) << fixed << arr[i].cgpa << " | " << arr[i].rollNumber<<"        ";
            if (i == (pow(2, j) - 1))
            {
                cout << endl;
                j++;
            }
        }
    }
                                                  
};

int main()
{
    cout << "-- Welcome to Student Max Heap repository --\n";
    bool flag = 1;
    int i;
    string name;
    double cgpa;
    int rollNumber;
    Student s;
    StudentMaxHeap record(20);
    while (flag)
    {
        cout << "\n\n1. Insert a new Student\n2. Remove (and display) the student with the Max CGPA";
        cout << "\n3. Display the list of students (Descending order of CGPA)";
        cout << "\n4. Display the list of students (Level-order traversal)";
        cout << "\n5. Exit";
        cout << "\n\nEnter your choice: ";
        cin >> i;
        
        
        if(i == 1)
        {
            cout << "Enter student's name: ";
            cin >> name;
            cout << "Enter student's cgpa: ";
            cin >> cgpa;
            cout << "Enter student's roll number: ";
            cin >> rollNumber;
            record.insert(rollNumber, name, cgpa);
        }
        else if (i == 2)
        {
            s = record.extractTopper();
            cout << "\nRoll number: " << s.rollNumber;
            cout << "\nName: " << s.name;
            cin.ignore();
            cout << "\nCGPA: " << s.cgpa;
        }
        else if (i == 3)
        {
            record.displayStudentList();
        }
        else if (i == 4)
        {
            record.printHeap();
        }
        else if(i == 5)
            flag = 0;
        else
            cout << "Choice can only be a number between 1 to 5.";

    }

}

Aucun commentaire:

Enregistrer un commentaire