class variable trouble

I am having some trouble on a project and its probably something simple but I cant figure it out. It is my first time working with classes and I've been messing with it all day so the code might be a little off. Anyway my problem is I am suppose to make a class that can add or subtract two polynomials, or multiply a polynomial by a constant, but I cant get the member class variables to change. After I input the degree and the constants and the class goes to the next object everything resets to zero. I made a separate code that used functions instead and everything worked fine so I just need to know how to change the member class variables so when I input the numbers in they will work with every object in the class.
Here's some of my code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
class Polynomial10
{
    private:
        double coef[9];
        int degree;
    public:
        Polynomial10();//Constructor for a new Polynomial10
        Polynomial10(int d, double c[]);
        int getDegree(){return degree;};
        void print(); //Print the polynomial in standard form
        void read(); //Read a polynomial from the user
        void add(); //Add a polynomial
        void multc(); //Multiply the poly by scalar
        void subtract(); //Subtract polynom
};
Polynomial10::Polynomial10()
{
    degree=0;
    for (int i=0; i<9; i++)
    {
        coef[i]=0;
    }
}

Polynomial10::Polynomial10(int d, double c[]) //I don't think this is needed
{
    degree=d;
    for (int i=0; i<9; i++)
    {
        coef[i]=c[i];
    }
}

void Polynomial10::read()
{

    do
       {
           cout << "Enter degree of a polynom between 1 and 10 : ";
           cin >> degree;
       } while (degree < 1 || degree > 10);

    cout << "Enter space separated coefficients starting from highest degree\n";
    for (int i=0; i<=degree; i++)
    {
        cin >> coef[i];
    }
    Polynomial10 a(degree,coef); // I was just screwing around here hoping  //something would work
}

void Polynomial10::print()
{
    int i;
    int m;
    int n=degree;
    m=degree;
//after this it's a lot of code to make the output look nice

int main ()
{
    cout << "\nCGS 2421: The Polynomial10 class\n\n" << endl;
    Polynomial10 polydgree;
    Polynomial10 prnt;
    Polynomial10 rd;
    Polynomial10 plus;
    Polynomial10 subtrct;
    Polynomial10 multic;

    int enteredone;
    int choice;

    enteredone=0;

do {
    choice=choicemenu();

    switch(choice)
        {

            case 0:
                return 0;
            case 1:
                enteredone=1;
                rd.read();
                break;
            case 2:
                if ( enteredone != 1 )
                {
                    cout << "Enter the polynomial first" << endl;
                    break;
                }
                if ( enteredone == 1 )
                {
                    prnt.print();
                    break;
                }
            case 3:
                if ( enteredone != 1 )
                {
                    cout << "Enter the polynomial first" << endl;
                    break;
                }
                if ( enteredone == 1 )
                {
                    plus.add();
                    break;
                }
            case 4:
                subtrct.subtract();
                break;
            case 5:
                multic.multc();
                break;
        }

    } while (choice != 0);
}


Also could someone tell be the benefits of having this instead of what I have.
1
2
3
void add(const Polynomial10& pol);
void multc(double factor);
void subtract(const Polynomial10& pol); 


Thank you for your help.
Last edited on
I figured it out and its really stupid. Its was a problem in my main function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Polynomial10 polydgree;
Polynomial10 prnt;
Polynomial10 rd;
Polynomial10 plus;
Polynomial10 subtrct;
Polynomial10 multic;
.
.
.
rd.read();
prnt.print();
plus.add();
subtrct.subtract();
multic.multc();


The above code should be changed to a.X() like
1
2
3
4
5
6
7
8
9
Polynomial10 a;
.
.
.
a.read();
a.print();
a.add();
a.subtract();
a.multc();
Topic archived. No new replies allowed.