addition of polynomials

whats wrong with the following code?
#include<iostream>
using namespace std;
class polynomial;
class term
{
private:
friend class polynomial;
float coef;
int exp;
};
class polynomial
{
term *termarry;
int capacity,terms;
public:
polynomial();
void display(int);
void add(polynomial);
void newterm(float,int);
};
polynomial::polynomial()
{
capacity=1;
terms=0;
termarry=new term[capacity];
}
void polynomial::newterm(float coef,int ex)
{
if(terms==capacity)
{
capacity*=2;
term *temp=new term[capacity];
copy(termarry,termarry+terms,temp);
termarry=temp;
delete temp;
}
termarry[terms].coef=coef;
termarry[terms++].exp=ex;
}
void polynomial:: add(polynomial b)
{
polynomial c;
int apos=0,bpos=0,cpos=0;
if(termarry[apos].exp==termarry[bpos].exp)
{
float t=(termarry[apos].coef)+(termarry[bpos].coef);
c.newterm(t,termarry[apos].exp);
apos++; bpos++;cpos++;
}
else if(termarry[apos].exp<b.termarry[bpos].exp)
{
c.newterm(b.termarry[bpos].coef,b.termarry[bpos].exp);
bpos++;cpos++;
}
else
{
c.newterm(termarry[apos].coef,termarry[apos].exp);
apos++;
cpos++;
}
for(;apos<terms;apos++)
{
c.newterm(termarry[apos].coef,termarry[apos].exp);
cpos++;
}
for(;bpos<b.terms;bpos++)
{
c.newterm(b.termarry[bpos].coef,b.termarry[bpos].exp);
cpos++;
}
c.display(cpos);
}
void polynomial::display(int pos)
{
int i;
for(i=0;i<pos;i++)
{
cout<<termarry[i].coef<<" "<<termarry[i].exp<<"+";
}
}

int main()
{
polynomial a,b;
float coef1,coef2;
int e1=0,e2=0,l1,l2;
cout<<"Enter lowest deg: ";
cin>>l1;
cout<<"\nEnter first polynomial: ";
while(1)
{
cin>>coef1>>e1;
a.newterm(coef1,e1);
if(e1==l1)
break;
}
cout<<"Enter lowest deg: ";
cin>>l2;
cout<<"\nEnter second polynomial: ";
while(1)
{
cin>>coef2>>e2;
b.newterm(coef2,e2);
if(e2==l2)
break;
}
cout<<"\nPolynomial after addition: \n";
a.add(b);
return 0;
}


The only problem I see at the moment is there is no explanation of any of the code. What is wrong with how you are asking for help? In other words could you please tell us what you have tried to reach a solution? Could you tell us what you think may be wrong? Could you tell us more about when you started noticing a problem or where the problem might be? Could you at least give enough information so that other users can ask you questions that could lead to a solution? At the moment it does not look like you did not try very much to find a solution, which is not very motivating to someone who is trying to help.

So even without the explanation, I happen to be in a good mood. So I did some work on it anyways. I went to ideone to find out if anything was wrong with the code: http://ideone.com/cvSx7N
It appears to run, but has logical errors on how the polynomial is displayed. I would recommend outputting the polynomial a and b, before they are added. This would be to make sure the input works correctly. Let me know how that goes, and I will keep heling you if you have any other problems.
Topic archived. No new replies allowed.