c++ - Overloading operator trouble -
hello i'm trying create program reads in series of infix expressions file until reaches semi-colon or period (a+b.), , output postfix of expression file. i'm having trouble getting overloaded operators work correctly. can give me tips on what's wrong them?
std::istream& operator>>(std::istream& in, expression& x) { char y; { in >> y; x.ifix += y; if (y == '.') { x.last = true; } } while (y!= ';' || y!= '.'); x.converttopostfix(); return in; } std::ostream& operator<<(std::ostream& out, const expression& x) { out << x.pfix; return out; }
this expression true:
while (y != ';' || y != '.'); if y ';' , y can not '.', second half true. same other way around when y '.'.
you want
while (y != ';' && y != '.'); or, if want express more human:
while (!(y == ';' || y == '.')); in other words, "do stuff while y not equal semicolon or dot".
this common mistake when combining conditions, , 1 worth spending time understand, can spot in future - because make mistake again, guarantee it.
Comments
Post a Comment