Some background information, for a homework assignment I had to write a polish notation calculator using binary trees, for this to work I had to parse command line input so that it would properly build the binary tree and then go over it to give a valid answer to the mathematical expression that was entered.
For the parsing I used a std::stringstream so that I would easily be able to convert the std::string I was handed into a valid float (or integer, double). The issue I ran across was the following code, which has the error showcased and how I solved the issue. I was hoping that somebody where would be able to tell me if I was doing something wrong and .clear() is not correct, or if this is a bug in the standard library in the way it handles this particular input (only happens for + and -).
#include <iostream> #include <sstream> #include <string> int main() { std::string mystring("+"); int num; char op; std::stringstream iss(mystring); iss >> num; // Seems it is not a number if (iss.fail()) { // This part does not work as you would expect it to // We clear the error state of the stringstream iss.clear(); std::cout << "iss fail bit: " << iss.fail() << std::endl; iss.get(op); std::cout << "op is: " << op << " iss is: " << iss.str() << std::endl; std::cout << "iss fail bit: " << iss.fail() << std::endl; // This however works as you would expect it to std::stringstream oss(iss.str()); std::cout << "oss fail bit: " << oss.fail() << std::endl; oss.get(op); std::cout << "op is: " << op << " oss is: " << oss.str() << std::endl; std::cout << "oss fail bit: " << oss.fail() << std::endl; } else { // We got a number } } Sample output from the program:
iss fail bit: 0 op is: iss is: + iss fail bit: 1 oss fail bit: 0 op is: + oss is: + oss fail bit: 0 Maybe you guys will see something I missed, or if this is indeed a bug higher up beyond my program, in which case pointers as to where to report this would be greatly appreciated.