How can I fix my program from crashing in C++?
<p>I'm very new to programming and I am trying to write a program that adds and subtracts polynomials. My program sometimes works, but most of the time, it randomly crashes and I have no idea why. It's very buggy and has other problems I'm trying to fix, but I am unable to really get any further coding done since it crashes. I'm completely new here but any help would be greatly appreciated.</p>

<p>Here's the code:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;cstdlib&gt;

using namespace std;

int getChoice();
class Polynomial10
{
private:
    double* coef;
    int degreePoly;

public:
    Polynomial10(int max); //Constructor for a new Polynomial10
    int getDegree(){return degreePoly;};
    void print(); //Print the polynomial in standard form
    void read(); //Read a polynomial from the user
    void add(const Polynomial10&amp; pol); //Add a polynomial
    void multc(double factor); //Multiply the poly by scalar
    void subtract(const Polynomial10&amp; pol); //Subtract polynom
};

void Polynomial10::read()
{
    cout &lt;&lt; "Enter degree of a polynom between 1 and 10 : ";
    cin &gt;&gt; degreePoly;

    cout &lt;&lt; "Enter space separated coefficients starting from highest degree" &lt;&lt; endl;
    for (int i = 0; i &lt;= degreePoly; i++) cin &gt;&gt; coef[i];
}

void Polynomial10::print()
{
    for (int i = 0;i &lt;= degreePoly; i++) {
       if (coef[i] == 0) cout &lt;&lt; "";
       else if (i &gt;= 0) {
           if (coef[i] &gt; 0 &amp;&amp; i != 0) cout&lt;&lt;"+";
           if ((coef[i] != 1 &amp;&amp; coef[i] != -1) || i == degreePoly) cout &lt;&lt; coef[i];
           if ((coef[i] != 1 &amp;&amp; coef[i] != -1) &amp;&amp; i != degreePoly ) cout &lt;&lt; "*";
           if (i != degreePoly &amp;&amp; coef[i] == -1) cout &lt;&lt; "-";
           if (i != degreePoly) cout &lt;&lt; "x";
           if ((degreePoly - i) != 1 &amp;&amp; i != degreePoly) {
               cout &lt;&lt; "^";
               cout &lt;&lt; degreePoly-i;
           }
       }
   }
}

void Polynomial10::add(const Polynomial10&amp; pol)
{
    for(int i = 0; i&lt;degreePoly; i++) {
        int degree = degreePoly;
        coef[degreePoly-i] += pol.coef[degreePoly-(i+1)];
    }
}

void Polynomial10::subtract(const Polynomial10&amp; pol)
{
    for(int i = 0; i&lt;degreePoly; i++) {
        coef[degreePoly-i] -= pol.coef[degreePoly-(i+1)];
    }
}

void Polynomial10::multc(double factor)
{
    //int degreePoly=0;
    //double coef[degreePoly];
    cout &lt;&lt; "Enter the scalar multiplier : ";
    cin &gt;&gt; factor;
    for(int i = 0; i&lt;degreePoly; i++) coef[i] *= factor;
}

Polynomial10::Polynomial10(int max)
{
    degreePoly = max;
    coef = new double[degreePoly];
    for(int i; i &lt; degreePoly; i++) coef[i] = 0;
}

int main()
{
    int choice;
    Polynomial10 p1(1),p2(1);
    cout &lt;&lt; endl &lt;&lt; "CGS 2421: The Polynomial10 Class" &lt;&lt; endl &lt;&lt; endl &lt;&lt; endl;
    cout
        &lt;&lt; "0. Quit\n"
        &lt;&lt; "1. Enter polynomial\n"
        &lt;&lt; "2. Print polynomial\n"
        &lt;&lt; "3. Add another polynomial\n"
        &lt;&lt; "4. Subtract another polynomial\n"
        &lt;&lt; "5. Multiply by scalar\n\n";

    int choiceFirst = getChoice();
    if (choiceFirst != 1) {
        cout &lt;&lt; "Enter a Polynomial first!";
    }
    if (choiceFirst == 1) {choiceFirst = choice;}

    while(choice != 0) {
        switch(choice) {
            case 0:
                return 0;
            case 1:
                p1.read();
                break;
            case 2:
                p1.print();
                break;
            case 3:
                p2.read();
                p1.add(p2);
                cout &lt;&lt; "Updated Polynomial: ";
                p1.print();
                break;
            case 4:
                p2.read();
                p1.subtract(p2);
                cout &lt;&lt; "Updated Polynomial: ";
                p1.print();
                break;
            case 5:
                p1.multc(10);
                cout &lt;&lt; "Updated Polynomial: ";
                p1.print();
                break;
        }
        choice = getChoice();
    }
    return 0;
}

int getChoice()
{
    int c;
    cout &lt;&lt; "\nEnter your choice : ";
    cin &gt;&gt; c;
    return c;
}
</code></pre>
