0

So here is my Polynomial class. I made sure to initialize my pointers with dynamic memory whenever I delete them but the destructor is still initiating a breakpoint. I cannot figure out what I am doing wrong. I need to know the solution as my exam preparation depends on it. Don't roast me for using friend, I am only using it as it was a requirement for my assignment. I only know basic OOP concepts as I am still learning. Please try to avoid using templates etc for answer. Destructor at line 665. Dummy Input: + 2x^2 + 3x^1 + 1x^0 NOTE: When the destructor is commented the output is correct. Output at the end.

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

class Polynomial {
private:
    //Coefficient array
    long double* coefficients;

    //Exponent Array
    long double* exponents;

    /*We do not provide a separate set function for size as size is NOT independent. It is linked with either array. It also functions as a
    reference for all the elements in each array. If size is changed without changing the arrays to match then size would just be meaningless
    and would have no use to even warrant existing (size is just like me fr fr)*/
    long long size;

    //Negative or positive checks
    string signs;

    //The variable chosen as the default
    char variable;
public:
    //My constructor without arguments so that a variable can be initialized without putting values
    Polynomial()
    {
        coefficients = nullptr;
        exponents = nullptr;
        coefficients = new long double[4] { 0 };
        exponents = new long double[4] { 0 };
        size = 4;
        variable = 'x';
        signs.erase();
        signs += "++++";
    }

    //My constructor with arguments that takes arrays as pointers and their sizes as it's impossible to check the size of dynamic arrays through sizeof
    Polynomial(long double* tempCoefficients, long long coefficientSize, long double* tempExponents, long long exponentSize, char tempVariable, string tempSigns)
    {
        //Only if the sizes are correct
        if (coefficientSize == exponentSize && coefficientSize > 0 && exponentSize > 0 && tempSigns.length() == coefficientSize)
        {
            coefficients = nullptr;
            exponents = nullptr;
            coefficients = new long double[exponentSize];
            exponents = new long double[exponentSize];
            for (long long loop = 0; loop < exponentSize; loop++)
            {
                coefficients[loop] = tempCoefficients[loop];
            }
            for (long long loop = 0; loop < exponentSize; loop++)
            {
                exponents[loop] = tempExponents[loop];
            }
            size = exponentSize;
            variable = tempVariable;
            signs.erase();
            signs += tempSigns;
        }
        else
        {
            coefficients = nullptr;
            exponents = nullptr;
            coefficients = new long double[4] { 0 };
            exponents = new long double[4] { 0 };
            size = 4;
            variable = 'x';
            signs.erase();
            signs += "+++";
        }
    }
    //Coefficient array setter
    void setCoefficients(long double* tempCoefficients, long long coefficientSize)
    {
        if (coefficientSize == size && coefficientSize > 0)
        {
            delete[] coefficients;
            coefficients = nullptr;
            coefficients = new long double[coefficientSize];
            for (long long loop = 0; loop < coefficientSize; loop++)
            {
                coefficients[loop] = tempCoefficients[loop];
            }
        }
    }
    //Exponent array setter
    void setExponents(long double* tempExponents, long long exponentSize)
    {
        delete[] exponents;
        exponents = nullptr;
        exponents = new long double[exponentSize];
        for (long long loop = 0; loop < exponentSize; loop++)
        {
            exponents[loop] = tempExponents[loop];
        }
    }
    //Variable setter
    void setVariable(char tempVariable)
    {
        variable = tempVariable;
    }
    //Signs setter
    void setSigns(string tempSigns)
    {
        if (tempSigns.length() == size)
        {
            signs.erase();
            signs += tempSigns;
        }
    }
    //Setter similar to the constructor
    void setAll(long double* tempCoefficients, long long coefficientSize, long double* tempExponents, long long exponentSize, char tempVariable, string tempSigns)
    {
        if (coefficientSize == exponentSize && coefficientSize > 0 && exponentSize > 0 && tempSigns.length() == coefficientSize)
        {
            delete[] coefficients, exponents;
            coefficients = nullptr;
            exponents = nullptr;
            coefficients = new long double[exponentSize];
            exponents = new long double[exponentSize];
            for (long long loop = 0; loop < exponentSize; loop++)
            {
                coefficients[loop] = tempCoefficients[loop];
            }
            for (long long loop = 0; loop < exponentSize; loop++)
            {
                exponents[loop] = tempExponents[loop];
            }
            size = exponentSize;
            variable = tempVariable;
            signs.erase();
            signs += tempSigns;
        }
    }
    //All getter. Pretty straightforward
    long double* getCoefficients()
    {
        return coefficients;
    }
    long double* getExponents()
    {
        return exponents;
    }
    long long getSize()
    {
        return size;
    }
    char getVariables()
    {
        return variable;
    }
    string getSigns()
    {
        return signs;
    }
    //Inputting the polynomial expression from the user
    void getPolynomialFromUser()
    {
        //Error checker bool
        bool check = false;
        //Temp input string
        string tempExpression;
        //Number of the inputted terms
        long long tempSize = 0;
        //Infinite loop to handle the error correction. Only breaks when all the conditions are satisfied
        while (1)
        {
            //Check if the loop has already been executed and need to reinput and provide a clean interface
            if (check)
            {
                cout << "Wrong Input! Please enter correctly\n";
            }
            //Reset the error checker
            check = false;
            //Asking the user to enter the input in a specified format. If he doesn't then tough luck (FOR HIM HAHAHAHAHAHHAHAHAHAAH)
            cout << "Please enter your polynomial expression e.g. (+ ax^2 + bx^1 + cx^0) or (+ ax^4) or any for that matter:\n";
            getline(cin, tempExpression);
            //Checking if the user entered anything other than numbers, variable, power indicator, spaces or signs
            for (long long loop = 0; loop < tempExpression.length(); loop++)
            {
                //Taking the NOT of anything correct found
                if (!(tempExpression[loop] == '^' || (tempExpression[loop] >= '0' && tempExpression[loop] <= '9')
                    || (tempExpression[loop] >= 'a' && tempExpression[loop] <= 'z') || tempExpression[loop] == ' '
                    || tempExpression[loop] == '+' || tempExpression[loop] == '-'))
                {
                    //Telling the loop that an oddity was found and need to reinput
                    check = true;
                    //Don't need to check more if oddity found even once
                    break;
                }
            }
            //Checking if anything other than numbers and spaces found repeating one after another
            for (long long loop = 0; loop < tempExpression.length(); loop++)
            {
                //If numbers and spaces not found
                if (!(tempExpression[loop] >= '0' && tempExpression[loop] <= '9') && tempExpression[loop] != ' ')
                {
                    //Check if the character repeats even once
                    char tempChar = tempExpression[loop];
                    if (tempExpression[loop + 1] == tempChar)
                    {
                        //Telling the loop that an oddity was found and need to reinput
                        check = true;
                        //Don't need to check more if oddity found even once
                        break;
                    }
                }

            }
            //For keeping track of everything that if everything was found in the correct order
            short termChecker = 0;
            //Check the whole string for oddities
            for (long long loop = 0; loop < tempExpression.length(); loop++)
            {
                //Check if the user entered any spaces at the start of the string or at the end of each term and ignore them.
                //termChecker is the key to all of it (I love termChecker like my own son ;( )
                if (tempExpression[loop] == ' ' && termChecker == 0)
                {
                    continue;
                }
                //After all the spaces have been covered check for the sign of the term
                if ((tempExpression[loop] == '-' || tempExpression[loop] == '+') && termChecker == 0)
                {
                    //If the sign is found right after the spaces then the input is correct. Oh and uh also moving to the next location
                    loop++;
                    //Ignoring all the spaces again
                    while (tempExpression[loop] == ' ')
                    {
                        loop++;
                    }
                    //Updating my termChecker to tell it that the first part was correct
                    termChecker = 1;
                    //The next characters should be the coefficients
                    if (tempExpression[loop] >= '0' && tempExpression[loop] <= '9' && termChecker == 1)
                    {
                        //Updating my termChecker to tell it that the this part is also correct
                        termChecker = 2;
                        //Cycling through the remaining digits of the coefficients
                        while (tempExpression[loop] >= '0' && tempExpression[loop] <= '9')
                        {
                            loop++;
                        }
                        //The next character should be the variable
                        if (termChecker == 2 && tempExpression[loop] >= 'a' && tempExpression[loop] <= 'z')
                        {
                            //Updating my termChecker to tell it that the this part is also correct
                            termChecker = 3;
                            //Switching to the next character to check
                            loop++;
                            //The next character should be the power indicator
                            if (termChecker == 3 && tempExpression[loop] == '^')
                            {
                                //Updating my termChecker to tell it that the this part is also correct
                                termChecker = 4;
                                //Switching to the next character to check
                                loop++;
                                //The next characters should be the exponents
                                if (termChecker == 4 && tempExpression[loop] >= '0' && tempExpression[loop] <= '9')
                                {
                                    //Updating my termChecker to tell it that the this part is also correct
                                    termChecker = 5;
                                    //Cycling through the remaining digits of the exponents
                                    while (tempExpression[loop] >= '0' && tempExpression[loop] <= '9')
                                    {
                                        loop++;
                                    }
                                    //Finally updating the size because one correct term has been read
                                    tempSize++;
                                }
                            }
                        }
                    }
                    //If all the conditions were not met then should reinput
                    if (termChecker != 5)
                    {
                        // Telling the loop that an oddity was found
                        check = true;
                        break;
                    }
                    //Resetting the termChecker for the next term 
                    termChecker = 0;
                } //If even the first condition is not met then we need to tell the loop
                else
                {
                    check = true;
                }

            }
            //If nothing was entered
            if (tempSize <= 0)
            {
                check = true;
            }
            //Finally applying the error checking
            if (check)
            {
                continue;
            }
            //If everything is perfect then continue
            break;
        }
        //Clean all my arrays and strings and also setting the size
        delete[] coefficients, exponents;
        coefficients = nullptr;
        exponents = nullptr;
        coefficients = new long double[tempSize] {0};
        exponents = new long double[tempSize] {0};
        size = tempSize;
        signs = "";
        //Running the same error checker loop but this time storing all the correct stuff
        short termChecker = 0;
        //The index for all my arrays
        long long tempLooper = 0;
        //Same loop
        for (long long loop = 0; loop < tempExpression.length(); loop++)
        {
            if (tempExpression[loop] == ' ' && termChecker == 0)
            {
                continue;
            }
            if ((tempExpression[loop] == '-' || tempExpression[loop] == '+') && termChecker == 0)
            {
                //This time storing the signs too as they are correct
                signs += tempExpression[loop];
                loop++;
                while (tempExpression[loop] == ' ')
                {
                    loop++;
                }
                termChecker = 1;
                if (tempExpression[loop] >= '0' && tempExpression[loop] <= '9' && termChecker == 1)
                {
                    termChecker = 2;
                    while (tempExpression[loop] >= '0' && tempExpression[loop] <= '9')
                    {
                        //This time storing the coefficients too as they are correct
                        coefficients[tempLooper] = (coefficients[tempLooper] * 10) + (tempExpression[loop] - 48);
                        loop++;
                    }
                    if (termChecker == 2 && tempExpression[loop] >= 'a' && tempExpression[loop] <= 'z')
                    {
                        //Only need to store the variable once
                        if (tempLooper == 0)
                        {
                            //This time storing the variable too as it is correct
                            variable = tempExpression[loop];
                        }
                        termChecker = 3;
                        loop++;
                        if (termChecker == 3 && tempExpression[loop] == '^')
                        {
                            termChecker = 4;
                            loop++;
                            if (termChecker == 4 && tempExpression[loop] >= '0' && tempExpression[loop] <= '9')
                            {
                                termChecker = 5;
                                while (tempExpression[loop] >= '0' && tempExpression[loop] <= '9')
                                {
                                    //This time storing the exponents too as they are correct
                                    exponents[tempLooper] = (exponents[tempLooper] * 10) + (tempExpression[loop] - 48);
                                    loop++;
                                }
                            }
                        }
                    }
                }
                //Increase for next term
                tempLooper++;
                //Reset for next term
                termChecker = 0;
            }

        }

    }

    void checkArrayValues()
    {
        cout << "Coefficients:" << endl;
        for (long long loop = 0; loop < size; loop++)
        {
            cout << coefficients[loop] << endl;
        }
        cout << endl << "Exponents" << endl;
        for (long long loop = 0; loop < size; loop++)
        {
            cout << exponents[loop] << endl;
        }
        cout << endl << "Signs" << endl;
        for (long long loop = 0; loop < size; loop++)
        {
            cout << signs[loop] << endl;
        }
        cout << endl << "Size:" << size << endl << "Variable:" << variable << endl;
    }

    Polynomial operator+(Polynomial a)
    {
        //For checking the greatest power
        long long powerCounter = exponents[0];
        //checking the greatest power in the first object
        for (long long loop = 1; loop < size; loop++)
        {
            if (powerCounter < exponents[loop])
            {
                powerCounter = exponents[loop];
            }
        }
        //checking the greatest power in the second object
        for (long long loop = 0; loop < a.size; loop++)
        {
            if (powerCounter < a.exponents[loop])
            {
                powerCounter = a.exponents[loop];
            }
        }
        //Creating a new array that contains one location for each power so that the sums can be stored
        long double* exponentArray = new long double[powerCounter + 1] {0};
        //Taking all the coefficients of the first object and adding them to their respective power locations in the exponent array
        for (long long loop = 0; loop < size; loop++)
        {
            //If the sign is positive then add the number
            if (signs[loop] == '+')
            {
                exponentArray[static_cast<long long>(exponents[loop])] += coefficients[loop];
            }
            //If the sign is negative then subtract the number
            else if (signs[loop] == '-')
            {
                exponentArray[static_cast<long long>(exponents[loop])] -= coefficients[loop];
            }
        }
        //Taking all the coefficients of the second object and adding them to their respective power locations in the exponent array
        for (long long loop = 0; loop < a.size; loop++)
        {
            //If the sign is positive then add the number
            if (a.signs[loop] == '+')
            {
                exponentArray[static_cast<long long>(a.exponents[loop])] += a.coefficients[loop];
            }
            //If the sign is negative then subtract the number
            else if (a.signs[loop] == '-')
            {
                exponentArray[static_cast<long long>(a.exponents[loop])] -= a.coefficients[loop];
            }
        }
        //Checking how many non zero terms occur after the sum of two objects
        long long tempCounter = 0;
        for (long long loop = 0; loop <= powerCounter; loop++)
        {
            if (exponentArray[loop] != 0)
            {
                tempCounter++;
            }
        }
        //creating the result arrays to be passed to the constructor later
        long double* finalCoefficients = new long double[tempCounter];
        long double* finalExponents = new long double[tempCounter];
        //Creating a copy of the number of non zero terms found and using it as index
        long long tempCounterCopy = tempCounter - 1;
        //Storing all the non zero terms from max index to smallest so that the terms occur from highest power to lowest down the array
        for (long long loop = 0; loop <= powerCounter; loop++)
        {
            if (exponentArray[loop] != 0)
            {
                finalCoefficients[tempCounterCopy--] = exponentArray[loop];
            }
        }
        //Resetting the index for the exponent array
        tempCounterCopy = tempCounter - 1;
        //Storing all the corresponding exponents from max index to smallest so that the terms occur from highest power to lowest down the array
        for (long long loop = 0; loop <= powerCounter; loop++)
        {
            if (exponentArray[loop] != 0)
            {
                finalExponents[tempCounterCopy--] = loop;
            }
        }
        //Storing the final signs separately at the end
        string tempSigns;
        for (long long loop = 0; loop < tempCounter; loop++)
        {
            //If the result is positive then the sign is +
            if (finalCoefficients[loop] > 0)
            {
                tempSigns += '+';
                //Remove the sign from the value as it has been extracted
                finalCoefficients[loop] = fabs(finalCoefficients[loop]);
            }
            //If the result is negative then the sign is -
            else if (finalCoefficients[loop] < 0)
            {
                tempSigns += '-';
                //Remove the sign from the value as it has been extracted
                finalCoefficients[loop] = fabs(finalCoefficients[loop]);
            }
        }
        Polynomial temp(finalCoefficients, tempCounter, finalExponents, tempCounter, variable, tempSigns);
        delete[] finalCoefficients, finalExponents, exponentArray;
        finalCoefficients = nullptr;
        finalExponents = nullptr;
        exponentArray = nullptr;
        return temp;
    }

    Polynomial operator-(Polynomial a)
    {
        //For checking the greatest power
        long long powerCounter = exponents[0];
        //checking the greatest power in the first object
        for (long long loop = 1; loop < size; loop++)
        {
            if (powerCounter < exponents[loop])
            {
                powerCounter = exponents[loop];
            }
        }
        //checking the greatest power in the second object
        for (long long loop = 0; loop < a.size; loop++)
        {
            if (powerCounter < a.exponents[loop])
            {
                powerCounter = a.exponents[loop];
            }
        }
        //Creating a new array that contains one location for each power so that the subtractions can be stored
        long double* exponentArray = new long double[powerCounter + 1] {0};
        //Taking all the coefficients of the first object and adding them to their respective power locations in the exponent array
        for (long long loop = 0; loop < size; loop++)
        {
            //If the sign is positive then add the number
            if (signs[loop] == '+')
            {
                exponentArray[static_cast<long long>(exponents[loop])] += coefficients[loop];
            }
            //If the sign is negative then subtract the number
            else if (signs[loop] == '-')
            {
                exponentArray[static_cast<long long>(exponents[loop])] -= coefficients[loop];
            }
        }
        //Taking all the coefficients of the second object and subtracting them from their respective power locations in the exponent array
        for (long long loop = 0; loop < a.size; loop++)
        {
            //If the sign is positive then subtract the number
            if (a.signs[loop] == '+')
            {
                exponentArray[static_cast<long long>(a.exponents[loop])] -= a.coefficients[loop];
            }
            //If the sign is negative then add the number
            else if (a.signs[loop] == '-')
            {
                exponentArray[static_cast<long long>(a.exponents[loop])] += a.coefficients[loop];
            }
        }
        //Checking how many non zero terms occur after the subtraction of two objects
        long long tempCounter = 0;
        for (long long loop = 0; loop <= powerCounter; loop++)
        {
            if (exponentArray[loop] != 0)
            {
                tempCounter++;
            }
        }
        //creating the result arrays to be passed to the constructor later
        long double* finalCoefficients = new long double[tempCounter];
        long double* finalExponents = new long double[tempCounter];
        //Creating a copy of the number of non zero terms found and using it as index
        long long tempCounterCopy = tempCounter - 1;
        //Storing all the non zero terms from max index to smallest so that the terms occur from highest power to lowest down the array
        for (long long loop = 0; loop <= powerCounter; loop++)
        {
            if (exponentArray[loop] != 0)
            {
                finalCoefficients[tempCounterCopy--] = exponentArray[loop];
            }
        }
        //Resetting the index for the exponent array
        tempCounterCopy = tempCounter - 1;
        //Storing all the corresponding exponents from max index to smallest so that the terms occur from highest power to lowest down the array
        for (long long loop = 0; loop <= powerCounter; loop++)
        {
            if (exponentArray[loop] != 0)
            {
                finalExponents[tempCounterCopy--] = loop;
            }
        }
        //Storing the final signs separately at the end
        string tempSigns;
        for (long long loop = 0; loop < tempCounter; loop++)
        {
            //If the result is positive then the sign is +
            if (finalCoefficients[loop] > 0)
            {
                tempSigns += '+';
                //Remove the sign from the value as it has been extracted
                finalCoefficients[loop] = fabs(finalCoefficients[loop]);
            }
            //If the result is negative then the sign is -
            else if (finalCoefficients[loop] < 0)
            {
                tempSigns += '-';
                //Remove the sign from the value as it has been extracted
                finalCoefficients[loop] = fabs(finalCoefficients[loop]);
            }
        }
        Polynomial temp(finalCoefficients, tempCounter, finalExponents, tempCounter, variable, tempSigns);
        delete[] finalCoefficients, finalExponents, exponentArray;
        finalCoefficients = nullptr;
        finalExponents = nullptr;
        exponentArray = nullptr;
        return temp;
    }

    void operator=(Polynomial a)
    {
        //Cleaning the arrays
        delete[] coefficients, exponents;
        coefficients = nullptr;
        exponents = nullptr;
        //Making new arrays of correct size
        coefficients = new long double[a.size];
        exponents = new long double[a.size];
        //Copying all the values from the passed arrays
        for (long long loop = 0; loop < a.size; loop++)
        {
            coefficients[loop] = a.coefficients[loop];
        }
        for (long long loop = 0; loop < a.size; loop++)
        {
            exponents[loop] = a.exponents[loop];
        }
        //Copying the rest of the values
        size = a.size;
        variable = a.variable;
        signs.erase();
        signs += a.signs;
    }

    void display()
    {
        //Displaying the data
        cout << "Your polynomial is: " << endl;
        bool check = false;
        for (long long loop = 0; loop < size; loop++)
        {
            //Only display the terms that are non zero
            if (coefficients[loop] != 0)
            {
                check = true;
                cout << signs[loop] << " " << coefficients[loop] << variable << "^" << exponents[loop] << "  ";
            }
        }
        //If all terms are zero then default case
        if (!check)
        {
            cout << " + 0" << variable << "^0";
        }

    }

    friend Polynomial operator*(Polynomial a, Polynomial b);
    friend void operator+=(Polynomial& a, Polynomial b);

    //Deleting the dynamic pointers
    ~Polynomial()
    {
        delete[] coefficients;
        delete[] exponents;
        coefficients = nullptr;
        exponents = nullptr;
    }
};


Polynomial operator*(Polynomial a, Polynomial b)
{
    //Making temporary arrays of correct size(number of terms of first) * (number of terms of second)
    long double* tempCoefficients = new long double[a.size * b.size] { 0 };
    long double* tempExponents = new long double[a.size * b.size] { 0 };
    //The new size
    long long tempSize = a.size * b.size;
    //Same variable
    char tempVariable = a.variable;

    string tempSigns;
    long long tempIndex = 0;
    //Multiplying each terms of first object with each term of second object
    for (long long loop = 0; loop < a.size; loop++)
    {
        for (long long loop2 = 0; loop2 < b.size; loop2++)
        {
            //If both positive then result is positive
            if (a.signs[loop] == '+' && b.signs[loop2] == '+')
            {
                tempSigns += '+';
            }
            //If both negative then result is positive
            else if (a.signs[loop] == '-' && b.signs[loop2] == '-')
            {
                tempSigns += '+';
            }
            //If signs are different then the answer is negative
            else if (a.signs[loop] == '-' && b.signs[loop2] == '+' || a.signs[loop] == '+' && b.signs[loop2] == '-')
            {
                tempSigns += '-';
            }
            //Calculating the actual value
            tempCoefficients[tempIndex] = a.coefficients[loop] * b.coefficients[loop2];
            tempExponents[tempIndex++] = a.exponents[loop] + b.exponents[loop2];
        }
    }
    Polynomial temp(tempCoefficients, tempSize, tempExponents, tempSize, tempVariable, tempSigns);
    delete[] tempCoefficients, tempExponents;
    tempCoefficients = nullptr;
    tempExponents = nullptr;
    return temp;
}

void operator+=(Polynomial& a, Polynomial b)
{
    //Doing the same thing done in the + function
    long long powerCounter = a.exponents[0];
    for (long long loop = 1; loop < a.size; loop++)
    {
        if (powerCounter < a.exponents[loop])
        {
            powerCounter = a.exponents[loop];
        }
    }
    for (long long loop = 0; loop < b.size; loop++)
    {
        if (powerCounter < b.exponents[loop])
        {
            powerCounter = b.exponents[loop];
        }
    }
    long double* exponentArray = new long double[powerCounter + 1] {0};
    for (long long loop = 0; loop < a.size; loop++)
    {
        if (a.signs[loop] == '+')
        {
            exponentArray[static_cast<long long>(a.exponents[loop])] += a.coefficients[loop];
        }
        else if (a.signs[loop] == '-')
        {
            exponentArray[static_cast<long long>(a.exponents[loop])] -= a.coefficients[loop];
        }
    }
    for (long long loop = 0; loop < b.size; loop++)
    {
        if (b.signs[loop] == '+')
        {
            exponentArray[static_cast<long long>(b.exponents[loop])] += b.coefficients[loop];
        }
        else if (b.signs[loop] == '-')
        {
            exponentArray[static_cast<long long>(b.exponents[loop])] -= b.coefficients[loop];
        }
    }
    long long tempCounter = 0;
    for (long long loop = 0; loop <= powerCounter; loop++)
    {
        if (exponentArray[loop] != 0)
        {
            tempCounter++;
        }
    }
    long double* finalCoefficients = new long double[tempCounter];
    long double* finalExponents = new long double[tempCounter];
    long long tempCounterCopy = tempCounter - 1;
    for (long long loop = 0; loop <= powerCounter; loop++)
    {
        if (exponentArray[loop] != 0)
        {
            finalCoefficients[tempCounterCopy--] = exponentArray[loop];
        }
    }
    tempCounterCopy = tempCounter - 1;
    for (long long loop = 0; loop <= powerCounter; loop++)
    {
        if (exponentArray[loop] != 0)
        {
            finalExponents[tempCounterCopy--] = loop;
        }
    }
    string tempSigns;
    for (long long loop = 0; loop < tempCounter; loop++)
    {
        if (finalCoefficients[loop] > 0)
        {
            tempSigns += '+';
            finalCoefficients[loop] = fabs(finalCoefficients[loop]);
        }
        else if (finalCoefficients[loop] < 0)
        {
            tempSigns += '-';
            finalCoefficients[loop] = fabs(finalCoefficients[loop]);
        }
    }

    //Cleaning the arrays
    delete[] a.coefficients, a.exponents;
    a.coefficients = nullptr;
    a.exponents = nullptr;
    //Making new arrays of correct size
    a.coefficients = new long double[tempCounter];
    a.exponents = new long double[tempCounter];
    //Copying all the values from temporary arrays
    for (long long loop = 0; loop < tempCounter; loop++)
    {
        a.coefficients[loop] = finalCoefficients[loop];
    }
    for (long long loop = 0; loop < tempCounter; loop++)
    {
        a.exponents[loop] = finalExponents[loop];
    }
    //Copying the rest of the values
    a.size = tempCounter;
    a.signs.erase();
    a.signs += tempSigns;
    delete[] finalCoefficients, finalExponents, exponentArray;
    finalCoefficients = nullptr;
    finalExponents = nullptr;
    exponentArray = nullptr;
}
int main()
{
    //Testing
    Polynomial a;
    Polynomial b;
    Polynomial c;
    a.getPolynomialFromUser();
    b.getPolynomialFromUser();
    cout << "\nAddition:" << endl;
    (a + b).display();
    cout << "\nSubtraction:" << endl;
    (a - b).display();
    cout << "\nMultiplication:" << endl;
    (a * b).display();
    cout << "\nAssignment c = a:" << endl;
    c = a;
    c.display();
    cout << "\nAddition Assignment c += a:" << endl;
    c += a;
    c.display();
    cout << endl;
    return 0;
}


Without Destructor: enter image description here With Destructor: enter image description here

Tried giving polynomials and was expecting correct operator execution and outputs.

Fahad Zia
  • 1
  • 1
  • Probably a double delete from having a raw pointer and not observing the rule of three. Maybe not, too much code to read. You should spend some time narrowing it down. `delete[] finalCoefficients, finalExponents, exponentArray;` and `delete[] coefficients, exponents;` do not do what you think they do. There are no shortcuts. – Retired Ninja Nov 19 '22 at 19:10
  • Checked every delete statement. Either the temporary pointers are destroyed or the private variables are reinitialized. Still can't seem to find the problem. – Fahad Zia Nov 19 '22 at 19:36
  • Read [What is The Rule of Three?](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three/) then fix all of the warnings in your code: https://godbolt.org/z/KeTGrn75P If your compiler isn't warning you about these things then turn the warning level up. The sample input you showed isn't accepted so I wasn't able to see any runtime errors. – Retired Ninja Nov 19 '22 at 19:53
  • I am reading the thread and I am trying to modify my code according to it by returning everything by reference and passing my objects by reference and now I am getting a new batch of errors. I will try to fix it and see what happens. + 2x^2 + 3x^1 + 1x^0 Just copy paste this sample. I tried pasting again and it still accepts it. You might have copied it incompletely. – Fahad Zia Nov 19 '22 at 20:40
  • Sorry for taking too much of your time. Here is the link of working online compiler. It's executing. https://onlinegdb.com/moD-H8ymC . – Fahad Zia Nov 19 '22 at 20:53
  • Rule of 3 is clutch – Fahad Zia Nov 19 '22 at 21:59
  • Rule of zero is better. Use `std::vector` instead of raw pointers. – Retired Ninja Nov 20 '22 at 00:18
  • Yes Yes but still need to learn – Fahad Zia Nov 21 '22 at 14:05

1 Answers1

0

Found the solution. Needed to pass the objects by reference and as const for the assignment and copy constructor.

Fahad Zia
  • 1
  • 1