it hangs

Hi! I'm new at C++ and I'm working on a project where it is supposed to add, subtract, multiply and divide polynomials. The problem is, when I compile it, it does not show an error but when I run it, it hangs. It says that it stopped working. Before, when I have this kind of problem, it's because of an infinite loop. But now, I only used for loop and I cant seem to find my error.


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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <cstdlib>
#include <iostream>

using namespace std;

class PolynomialClass
{

    
    public:
        PolynomialClass();
        PolynomialClass (double *pD2, int newExp); // this will be called anytime with the ff initialization
        int Exponent(int newExp); 
        int Coefficient (int newCoef);  
        PolynomialClass(PolynomialClass&);
        void output(void); //because we only want to return a cascade
        ~PolynomialClass(); //deconstructor
        
		PolynomialClass operator +(const PolynomialClass&);
		PolynomialClass operator -(const PolynomialClass&);
		PolynomialClass operator *(const PolynomialClass&);
		PolynomialClass operator /(const PolynomialClass&);
		PolynomialClass& operator = (const PolynomialClass&);
		
        friend ostream& operator << (ostream&strm, const PolynomialClass);
        
        private:
        double *pDouble;
        int Exp;   
};

PolynomialClass::PolynomialClass()
{
    Exp = 0;
	pDouble = new double[Exp + 1]; // dynamic allocation
	pDouble[0] = 0;

};

PolynomialClass::PolynomialClass(PolynomialClass& copy)
{
    Exp=copy.Exp;
    copy.pDouble = new double [copy.Exp + 1];
    for (int i = 0; i < copy.Exp; i++)
        this->pDouble[i] = copy.pDouble[i];
    this->Exp = copy.Exp;
}

PolynomialClass& PolynomialClass::operator= (const PolynomialClass& rhs)
{
    double *poly = new double (Exp);
    for (int i = 0; i < rhs.Exp; i++)
        {poly[i] = rhs.pDouble [i];}
    delete [] pDouble;
    pDouble = poly;
    Exp = rhs.Exp;
    
    return *this;
}

int PolynomialClass:: Coefficient (int newCoef)
{
    newCoef= Exp+1;
    
    return newCoef;
}

int PolynomialClass::Exponent (int newExp)
{
    double *poly = new double(Exp);
    int i;
    for (i=0; i< Exp; i++)

    delete [] pDouble; //we have to delete pDouble; it is now a dangling pointer
    pDouble = poly;
    
     
    return newExp;
}

PolynomialClass:: PolynomialClass(double *pD2, int newExp)
{
    Exp = newExp;
    pDouble = new double[newExp+1];
    for (int i = 0; i < newExp; i++) 
        pDouble[i] = pD2[i];
  
        
};

PolynomialClass PolynomialClass:: operator +(const PolynomialClass& rhs)
{
    PolynomialClass plus;
    plus.pDouble = new double [this->Exp+1];    
        for (int m=this->Exp; m>=0; m--)
            {
                plus.pDouble[m]=pDouble[m]+rhs.pDouble[m];
                plus.Exp= this->Exp;
            }
       
    return plus;
};

PolynomialClass PolynomialClass:: operator -(const PolynomialClass& rhs)
{
    PolynomialClass minus;
    minus.pDouble= new double [this->Exp+1];    
        for (int m=this->Exp; m>=0; m--)
            {
                minus.pDouble[m]=pDouble[m]-rhs.pDouble[m];
                minus.Exp= this->Exp;
            }
        
    return minus;
    
};

PolynomialClass PolynomialClass:: operator *(const PolynomialClass& rhs)
{
    PolynomialClass times;
    int Exp= (this->Exp + rhs.Exp+1);
    times.Exp = (Exp + rhs.Exp);
            for( int m = 0; m <= Exp; m++ )
                for( int j = 0; j <= rhs.Exp; j++ )
                    times.pDouble[ m + j ] = pDouble[ m ] *rhs.pDouble[ j ];
            return times;
    
};

PolynomialClass PolynomialClass:: operator /(const PolynomialClass& rhs)
{
    PolynomialClass divide;
    int Exp= (Exp-rhs.Exp+1);
    for (int m=this->Exp; m>0; m--)
    break;
    
    return divide;
    
};

void PolynomialClass::output(void)
{
	for(int i=(Exp); i>0; i--)			
	{
        std::cout <<pDouble[i] << "x^" << i << " + " ;
    }
    std:: cout<<pDouble[0]<<endl;
    			
	return;
}

PolynomialClass::~PolynomialClass()//destructor
{ 
		delete [] pDouble;
}

int main()
{
    cout << "This program can perform the four (4) basic operations (plus, minus, multiplication, and division) of polynomials."<<endl<<endl;



    double Coeff1 [8]={5,4,3};
    PolynomialClass Poly1 (Coeff1, 3);
    cout << "Poly1=";
    Poly1.output();
    cout <<endl;

    double Coeff2 [5]={2,2};
    PolynomialClass Poly2 (Coeff2, 2);
    cout << "Poly2=";
    Poly2.output();
    cout <<endl;
    
    PolynomialClass Poly3;
    Poly3=Poly1+Poly2;
    
    cout << "The sum of Poly1+Poly2 is equal to: " << endl;
    Poly3.output();
    cout << endl;
    
    PolynomialClass Poly4;
    Poly4=Poly1-Poly2;
    
    cout << "The difference of Poly1+Poly2 is equal to: " << endl;
    Poly4.output();
    cout << endl;
    
    PolynomialClass Poly5;
    Poly5=Poly1*Poly2;
    
    cout << "The product of Poly1+Poly2 is equal to: " << endl;
    Poly5.output();
    cout << endl;
    
    PolynomialClass Poly6;
    Poly6=Poly1/Poly2;
    
    cout << "The quotient of Poly1/Poly2 is equal to: " << endl;
    Poly6.output();
    cout << endl;

	
   	system ("pause");
	return 0;
}
Last edited on
Please use [code][/code] tags around your code; it makes it easier to read. And have you tried using a debugger to figure out where it is hanging?
hi! I tried to debug it, but it says it's an error.
I am not sure you know what the process of "debugging" means.

First thing to understand is the difference between compiling & debugging.

Compilation is when the computer converts your code into a binary executable that the OS can execute. If there are errors in the code, the compiler reports these. This does not find runtime or logical error and is not debugging.

Debugging is the process of finding errors, by tracking the value of variables to see where they cause a problem. There are a few ways to do this:

1. Write down the values on paper (yes paper), keeping track of the program logic, and seeing how they change. This is probably the hardest way.

2. Put lots of print (cout or printf) statements, to see how the values change. This is easier.

3. Use a debugging program, either one built in to the IDE, or a command line one. The IDE version is easiest, but debugging from the cmd line is a very good skill to have.

With IDE debuggers, you can set breakpoints, have a watch list of variable values, step through the code 1 line at a time. This is the method of debugging.

The worst kind of problem to have is a logical error. This is where the logic is just plain wrong from the start - no amount of debugging will solve this. An example of logical error might be:

Apples + Bananas = how many Oranges?


Usually logic errors are much more subtle than this, perhaps a better example might be using the wrong trig function to calculate something.

Hope all goes well.
hi! thank you!

i compiled it and it shows no error anymore, that's why i think it must be the logic that's wrong. But, I dont know where to start looking so i tried debugging it.

when i try debugging, it says "error with debugging process: GDB error: error, No source file named mp6.cpp"

i tried looking for problems like mine and they said that i have to Generate debugging information to "yes" (which i did), but it didn't help.

thanks for your help! :)

Which IDE are you using? It sounds like a problem with the applications settings regarding the directory the file is in.

Hopefully someone knows the details of your IDE.

If you are brave you can try gdb from the cmd line. There is an article on this site on how to use it.

Edit : Apologies if my previous post was way too simplistic.
Last edited on
i'm using wxDevC++ (i'm not sure if this is what you are asking about, HAHA sorry).

I'll try gdb, i'm kinda desperate now. (project is due on monday and it's saturday night here already).

and, thanks so much for taking time to answer my questions. :)
Last edited on
Yes that's what I was asking "Integrated Development Environment"

Hopefully there is someone who knows the ins & outs of this program.

Sometimes the help is not that great with applications. Hopefully something on the web.

It would be really educational to try gdb from the cmd line.

thanks so much for taking time to answer my questions. :)


No worries, thank you, - as not that many people actually say thanks. :+D
Topic archived. No new replies allowed.