Adding two polynomials

Hye! I am trying to add two polynomials whose exponents and coefficients are entered by user himself.Later I have to multiply and subtract too! But right now its giving an error for adding.
"Unhandled exception thrown: read access violation.
this->ptr1 was 0x1110112". Kindly help me!

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
  #include <iostream>
using namespace std;
class Polynomial
{
public:
	int coff;
	int degree;
	int terms;
	int exp;
	int c = 1;
	int *ptr;
	int *ptr1;
	void setfun()
	{
		cout << "Enter terms in equation " << endl;
		cin >> terms;
		int *ptr = new int[terms];
		int *ptr1 = new int[terms];
		for (int i = 0; i + 1 <= terms; i++)
		{
			cout << "Enter coffecient" << c << endl;
			cin >> coff;
			ptr[i] = coff;
			cout << "Enter exponent for coffieient " << c << endl;
			cin >> exp;
			ptr1[i] = exp;
			c++;
		}
		for (int i = 0; i + 1 <= terms; i++)
		{
			cout << ptr[i] << "x" << "^" << ptr1[i];
			cout << "+";
		}
	}
	void operator + (Polynomial &b)
	{
		Polynomial result;
		for (int i = 0; i < terms; i++)
		{
			for (int j = 0; j < terms; j++)
			{
				if (ptr1[i] == b.ptr[j])
				{
					cout << ptr[i] + b.ptr[j] << "x" << "^" << "+";


				}
			}
		}
	}
};
int main()
{
	Polynomial a, b;
	a.setfun();
	b.setfun();
	a + b;
	system("pause");
}
Before delving into your code, please note you’re shadowing your class properties ‘ptr’ and ‘ptr1’ in your method setfun().
Also, the result of a + b will get lost.
Topic archived. No new replies allowed.