views:

980

answers:

1

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.

+2  A: 

When you say:

  iss.clear();
  std::cout << "iss fail bit: " << iss.fail() << std::endl;
  iss.get(op);

you are trying to read something that has already been read. You need to reset the streams read pointer:

  iss.clear();
  iss.seekg(0);    // start again
  std::cout << "iss fail bit: " << iss.fail() << std::endl;
  iss.get(op);
anon
However, at this point the + has not been read, since it is never extracted from the stream, since it is not a valid number. Why is the read pointer moved forward when it fails?
X-Istence
That's the magic of stream i/o, I'm afraid. You have to deal with things as they are, not as you would like them to be.
anon
X-Istence i think it is extracted because "+" is a valid part of a positive number. like "+10". anyway shouldn't it be "seekg" (seek get) instead of "seekp" (seek put) ?
Johannes Schaub - litb
You are right - I had seekg() in the code I wrote to test it. I don't know how the seekp() got in there!
anon