/I'm having trouble creating a constructor for degree and *coeffs as well as properly creating the dynamic arrays for the coefficients of the 2 polynomials a and b. I'm just concerned about getting the correct values.
My textbook only gives us eamples of constructos for eamples not well related to my HW question.Due tonight! any help would be much appreciated.
/

    #include <iostream>
    #include <cstdlib>
    using namespace std;
    struct Polynomial 
    {
       int degree;
       int *coeffs;
       Polynomial();
    };
    Polynomial* readPoly1();
    Polynomial* readPoly2();
    void outputPoly(Polynomial* p,char x);
    Polynomial* addPoly(Polynomial* a,Polynomial* b); 
    Polynomial* multPoly(Polynomial* a,Polynomial* b);
    void deletePoly(Polynomial* &p);


    int main()
    {
       Polynomial *a;
       Polynomial *b;
       a = readPoly1();
       b = readPoly2();
       if(a.coeffs == NULL || b.coeffs == NULL)
       {
          cout << "Error: Insuficient memory...exiting program..\n";
          exit(1);
       }

    }

    Polynomial::Polynomial()
    {
       a.degree = 0;
       b.degree = 0;
       a.coeffs = 0;
       b.coeffs = 0;
    }

    Polynomial* readPoly1()
    {
       int deg1;
       int* coefficients1;
       coefficients1 = new int[deg1+1];
       /*------------------------Read in first polynomial--------------------*/
       cout << "Enter degree of first polynomial:";
       cin >> deg1;
       cout << "Enter the coefficients of first polynomial starting with "
            << "the lowest degree term and ending with the highest degree term:";
       for(int i =0;i<deg1+1;i++)
       {
          cin >> coefficients1[i]
       }      
       /*---------------------------------------------------------------------*/
       a = new Polynomial;
       a -> degree = deg1;
       a -> coeffs = coefficients1;
       return a;
    }
    Polynomial* readPoly2()
    {
       int deg2;
       int* coefficients2;
       coefficients2 = new int[deg2+1];
       /*------------------------Read in second polynomial--------------------*/
       cout << "Enter degree of second polynomial:";
       cin >> deg2;
       cout << "Enter the coefficients of second polynomial starting with "
            << "the lowest degree term and ending with the highest degree term:";   
       for(int i =0;i<deg2+1;i++)
       {
          cin >> coefficients2[i];
       }  
       /*----------------------------------------------------------------------*/
       b = new Polynomial;
       b -> degree = deg2;
       b -> coeffs = coefficients2;
       return b;
    }

/*
//Here is my error list

C:\MinGW\bin\g++ -pedantic -Wall -Wextra A7.cpp -o A7.exe
A7.cpp: In function int main()': A7.cpp:24: error: request for membercoeffs' in a', which is of non-class typePolynomial'
A7.cpp:24: error: request for member coeffs' inb', which is of non-class type Polynomial*' A7.cpp: In constructorPolynomial::Polynomial()':
A7.cpp:34: error: a' was not declared in this scope A7.cpp:35: error:b' was not declared in this scope
A7.cpp: In function Polynomial* readPoly1()': A7.cpp:53: error: expected;' before '}' token
A7.cpp:55: error: a' was not declared in this scope A7.cpp: In functionPolynomial
readPoly2()':
A7.cpp:75: error: `b' was not declared in this scope
Exit code: 1
*/

i fixed missing ';', that error was not my problem though ofcoarse so ignore that error

Your constructor should look like this

Polynomial::Polynomial()
{
    degree = 0;
    coeffs = NULL;
}

i remade constructor, did i fix it?

Polynomial::Polynomial() : degree(0),coeffs(0)
{}

new error list:

C:\MinGW\bin\g++ -pedantic -Wall -Wextra A7.cpp -o A7.exe
A7.cpp: In function int main()': A7.cpp:26: error: request for membercoeffs' in a', which is of non-class typePolynomial'
A7.cpp:26: error: request for member coeffs' inb', which is of non-class type Polynomial*' A7.cpp: In functionPolynomial
readPoly1()':
A7.cpp:54: error: a' was not declared in this scope A7.cpp: In functionPolynomial* readPoly2()':
A7.cpp:74: error: `b' was not declared in this scope
Exit code: 1

thanks nathan! i've got rid of all my compiler errors but when i run program i get this error:

A7
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Exit code: 3

The problem looks like you are causing memory corruption. This coefficients1 = new int[deg1+1]; need to come after cin >> deg1; in your readpoly function. This is also the case with your second function. Right now you are initalizing your array with a number from a variable that has not been initalized yet. If you get the size from the user first then declare the array you should be okay. you also need to change line 55 to Polynomial * a = new Polynomial;. Just an FYI both functions do exactly the same thing so you dont really need both functions.

You rock kind sir

NOTE DISREGARD THIS MESSAGE, my error does not occur where i say it has

I'm having trouble copy the temporary array coefficients1 to a->coeffs in this fct.Even though the value at coeffients1[0] is valid,a->coeffs[0] gets set to zero when copied.
ie.highest degree is 1

coffients1[0] = 2,coefficients1[1]=1    
a->coeffs = coefficients
a->coeffs[0] = 0,a->coeffs[1] = 1





Polynomial* readPoly1()
{
   int deg1;

   /*------------------------Read in first polynomial--------------------*/
   cout << "Enter degree of first polynomial:";
   cin >> deg1;
   cout << "Enter the coefficients of first polynomial starting with\n"
        << "the lowest degree term and ending with the highest degree term\n";
   int* coefficients1;
   coefficients1 = new int[deg1+1];
   for(int i =0;i<deg1+1;i++)
   {
      cout << ":";
      cin >> coefficients1[i];// a test used to examine values of coefficients1
   }      
   /*---------------------------------------------------------------------*/
   Polynomial * a = new Polynomial;
   a -> degree = deg1;
   a -> coeffs = coefficients1;//Error here when coefficients copies to a->coeffs
   cout << "Degree:"<<a->degree;
   cout << "cofficients;";
   for(int j=0;j<a->degree+1;j++)//a test used to examine values of a->coeffs
   {cout << a->coeffs[j];
   }
   delete []coefficients1;
   return a;
}

Here's my problem. the fct readPoly gets the values proerly but when main calls the fct a and b don't recieve the values of p.

#include <iostream>
#include <cstdlib>
//Why can't we get help during labs.....
using namespace std;
struct Polynomial 
{
   int degree;
   int *coeffs;
   Polynomial();
};
Polynomial* readPoly();
Polynomial* readPoly2();
void outputPoly(Polynomial* p);
Polynomial* addPoly(Polynomial* a,Polynomial* b);
Polynomial* multPoly(Polynomial* a,Polynomial* b);
void deletePoly(Polynomial* &p);

int z = 1;//BAD PRACTICE, but i just wanted a quick way to identify which polynomial was being read into.
int main()
{
   Polynomial *a;
   Polynomial *b;
   Polynomial *c;
   a = new Polynomial;
   b = new Polynomial;
   int deg = 0;
   if(a->degree >= b->degree)
   {
      deg = a->degree;
   }
   else{deg = b->degree;}
   c = new Polynomial[deg+1];   

   a = readPoly();//ERROR:a does not equal the p that it was assigned
   cout << "\ncompleted readPoly(a)\n";
   b = readPoly();//ERROR:b does not equal the p that it was assigned
   c = a;
   cout << "\ncompleted readPoly(b)\n";
   if(a-> coeffs == NULL || b-> coeffs == NULL)
   {
      cout << "Error: Insuficient memory...exiting program..\n";
      exit(1);
   }
   cout << "\nInside main:\nDegree:"<<a->degree;
   cout << "\naCofficients:";
   for(int j=0;j<a->degree+1;j++)
   {cout << a->coeffs[j];
   }
   cout << endl;
     cout << "\nbCofficients:";
   for(int j=0;j<b->degree+1;j++)
   {cout << b->coeffs[j];
   }
   cout << endl;
   cout <<"c->degree:"<< c->degree<<"\nc->coeffs:";
   for(int j=0;j<c->degree+1;j++)
   {cout << c->coeffs[j];
   }
   outputPoly(a);
   outputPoly(b);
   cout << "\nAbout to enter addPoly(a,b)\n";
   c = addPoly(a,b);
   outputPoly(c);
   //deletePoly(p);
   return 0;
}

Polynomial::Polynomial() : degree(0),coeffs(NULL)//constructor
{}

Polynomial* readPoly()
{
   int deg;

   /*------------------------Read in polynomial---------------------------*/
   cout << "Enter degree of polynomial"<<z<<":";
   cin >> deg;
   cout << "Enter the coefficients of polynomial"<< z <<" starting with\n"
        << "the lowest degree term and ending with the highest degree term\n";
   int* coefficients;
   coefficients = new int[deg+1];
   for(int i =0;i<deg+1;i++)
   {
      cout << ":";
      cin >> coefficients[i];
   }      
   /*---------------------------------------------------------------------*/
   Polynomial * p = new Polynomial;
   p -> degree = deg;
   p -> coeffs = coefficients;
   cout << "Degree:"<<p->degree;
   cout << "\np->coeffs:";
   for(int j=0;j<p->degree+1;j++)//a test used to examine values of p->coeffs
   {cout << p->coeffs[j];
   }
   delete []coefficients;
   z++;
   return p;
}


Polynomial* addPoly(Polynomial* a,Polynomial* b)
{
   cout << "Program entered addPoly\n";
   int max = 0;
   int min = 0;
   int tracker = 0;
   if(a->degree > b-> degree)//Determines which polynomial has a higher degree of a and b, because c degree should match higher degree
   {
      max = a-> degree;
      min = b-> degree;      
   }
   else
   {
      max = b-> degree;
      min = a-> degree;
   }
   Polynomial* p;
   p = new Polynomial[max+1];
   for(int i = 0;i<min+1;i++)
   {
      p->coeffs[i] = a->coeffs[i] + b->coeffs[i];
      tracker = i;
   }
   if(a->degree > b-> degree)//Determines which polynomial has a higher degree of a and b, because c degree should match higher degree
   {
      for(int j = 0;j<max-min;j++)
      {
         p->coeffs[j+tracker+1] = a->coeffs[j+tracker+1];
      }
      p->degree = a->degree;
   }
   else
   {
      for(int j = 0;j<max-min;j++)
      {
         p->coeffs[j+tracker+1] = b->coeffs[j+tracker+1];
      }
      p->degree = b->degree;
   }
   return p;   
}


void outputPoly(Polynomial *p)
{
   int deg = p->degree;
   cout << "\nInside fct outputPoly:\nDegree:"<<p->degree;
   cout << "\nCofficients:";
   for(int j=0;j<p->degree+1;j++)
   {
      cout << p->coeffs[j];
   }
   cout << endl;
   for(int i=0;i<deg+1;i++)
   {
      cout << p->coeffs[deg - i] << "x^" <<deg-i;
      if(i<p->degree)
      {
         cout << "+";
      }
   }
   cout << "\nCompleted outputPoly\n";
   return;
}



void deletePoly(Polynomial* &p)
{
   delete []p;
   p = NULL;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.