I am writing a polynomial class but the code I have written to overload the ostream operator<< goes into a infinite loop for the first term. I tried to step through but Visual Studio 2010 goes into linker(?) code instead of only stepping through my code. (How to change the setting would help (: ) Anyway, it worked fine when I left it simple to just iterating down without much formatting. But this formatted polynomial I've made causes the infinite loop.

size = size of the coefficient array where the position in the array is the power

``````friend ostream &operator<<(ostream &Out, const Poly &rhs)
{
for(int i = (rhs.size - 1); i > 0; i--)
{
if(i = rhs.size - 1)
{
if(rhs.coeff[i] > 0)
Out << rhs.coeff[i] << "X^" << i << " ";
else
if(rhs.coeff[i] < 0)
Out << "-" << rhs.coeff[i] << "X^" << i;
}
else if(rhs.coeff[i] > 0)
Out << " + " << rhs.coeff[i] << "X^" << i;
else if(rhs.coeff[i] < 0)
Out << " - " << (rhs.coeff[i] * -1) << "X^" << i;
}
if(rhs.coeff > 0)
Out << " + " << rhs.coeff;
else if(rhs.coeff < 0)
Out << " - " << (rhs.coeff * -1);

return Out;
}``````

## Recommended Answers

The problem is here -> `if(i = rhs.size - 1)` . It should be -> `if(i == rhs.size - 1)`

## All 2 Replies

The problem is here -> `if(i = rhs.size - 1)` . It should be -> `if(i == rhs.size - 1)`

You are a golden god. Thanks!

Be a part of the DaniWeb community

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