How can I fix my program from crashing in C++?
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.
Here's the code:
#include <iostream>
#include <cstdlib>
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& pol); //Add a polynomial
void multc(double factor); //Multiply the poly by scalar
void subtract(const Polynomial10& pol); //Subtract polynom
};
void Polynomial10::read()
{
cout << "Enter degree of a polynom between 1 and 10 : ";
cin >> degreePoly;
cout << "Enter space separated coefficients starting from highest degree" << endl;
for (int i = 0; i <= degreePoly; i++) cin >> coef[i];
}
void Polynomial10::print()
{
for (int i = 0;i <= degreePoly; i++) {
if (coef[i] == 0) cout << "";
else if (i >= 0) {
if (coef[i] > 0 && i != 0) cout<<"+";
if ((coef[i] != 1 && coef[i] != -1) || i == degreePoly) cout << coef[i];
if ((coef[i] != 1 && coef[i] != -1) && i != degreePoly ) cout << "*";
if (i != degreePoly && coef[i] == -1) cout << "-";
if (i != degreePoly) cout << "x";
if ((degreePoly - i) != 1 && i != degreePoly) {
cout << "^";
cout << degreePoly-i;
}
}
}
}
void Polynomial10::add(const Polynomial10& pol)
{
for(int i = 0; i<degreePoly; i++) {
int degree = degr开发者_Python百科eePoly;
coef[degreePoly-i] += pol.coef[degreePoly-(i+1)];
}
}
void Polynomial10::subtract(const Polynomial10& pol)
{
for(int i = 0; i<degreePoly; i++) {
coef[degreePoly-i] -= pol.coef[degreePoly-(i+1)];
}
}
void Polynomial10::multc(double factor)
{
//int degreePoly=0;
//double coef[degreePoly];
cout << "Enter the scalar multiplier : ";
cin >> factor;
for(int i = 0; i<degreePoly; i++) coef[i] *= factor;
}
Polynomial10::Polynomial10(int max)
{
degreePoly = max;
coef = new double[degreePoly];
for(int i; i < degreePoly; i++) coef[i] = 0;
}
int main()
{
int choice;
Polynomial10 p1(1),p2(1);
cout << endl << "CGS 2421: The Polynomial10 Class" << endl << endl << endl;
cout
<< "0. Quit\n"
<< "1. Enter polynomial\n"
<< "2. Print polynomial\n"
<< "3. Add another polynomial\n"
<< "4. Subtract another polynomial\n"
<< "5. Multiply by scalar\n\n";
int choiceFirst = getChoice();
if (choiceFirst != 1) {
cout << "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 << "Updated Polynomial: ";
p1.print();
break;
case 4:
p2.read();
p1.subtract(p2);
cout << "Updated Polynomial: ";
p1.print();
break;
case 5:
p1.multc(10);
cout << "Updated Polynomial: ";
p1.print();
break;
}
choice = getChoice();
}
return 0;
}
int getChoice()
{
int c;
cout << "\nEnter your choice : ";
cin >> c;
return c;
}
When you create your objects with p1(1)
and p2(1)
the coef
array in each object is allocated to contain one element. Then in read()
you simply set degreePoly
to a (possibly higher) value but don't change the allocation of coef
. It will still contain only one element, but all coefficients are written to it, probably writing over the bounds of the array. Instead the old coef
should be freed and a new array of suitable size be allocated.
Also in add
and subtract
you are assigning to coefficients out of bounds (for i=0):
coef[degreePoly-i] -= pol.coef[degreePoly-(i+1)];
It also seems wrong mathematically to subtract the coefficients at index degreePoly-(i+1)
from those at index degreePoly-i
. Additionally the case that the two polygons might have different degrees is currently not handled.
One issue is the coef
allocation. You're allocating an array of degreePoly
=max
elements (1 for the two actual objects). But I think it should be max + 1
, since a n-degree polynomial has n + 1 coefficients. This off-by-one error appears elsewhere (e.g. add and mult). Also in the constructor, you are not initializing i
, so it has an undefined value, which alone means you can (probably will) write to memory you don't control.
Later, in your read code, you overwrite degreePoly
, but do not reallocate coef
, so it could be the wrong size, at least if you exceed your maximum. Also note that you are accepting degreePoly + 1
coefficients (which I think is correct) here. This means a max
of 1 in the constructor only corresponds to a degreePoly
of 0 in read! Entering 1 in read
will cause two values to be read, which will overflow the array.
Writing to invalid pointer locations and using uninitialized values causes undefined behavior, which is why it crashes sometimes.
if (choiceFirst == 1) {choiceFirst = choice;}
doesn't make sense since choice
is not initialized at this stage. You probably want:
if (choiceFirst == 1) {choice = choiceFirst;}
精彩评论