### 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206`` ``````#include #include 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 <
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.

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.