When I put something into a stringstream, let’s say a real number, if I then insert that stringstream object into cout…what am I looking at?
Usually I’m getting some strange number. Is this a memory location? Just curious.
It looks like the below comment hit it but here’s what I’m trying to do:
string stringIn;
stringstream holdBuff;
holdBuff << getline(cin, stringIn);
cout << holdBuff;
Basically I was just trying to see what holdBuff looked like once I inserted stringIn. I am trying to have the user enter a string and then I want to step through it looking for it’s contents and possilbly converting…
What do you think
is doing. The return type of
getlineis a reference to the streambeing read (
cin) in this case. Since there’s no<<defined whichtakes an
std::istreamas second argument, the compiler tries differentconversions: in C++11,
std::istreamhas an implicit conversion tobool, and in earlier C++, an implicit conversion tostd::ios*, orsomething similar (but the only valid use of the returned value is to
convert it to
bool). So you’ll either output1(C++11), or somerandom address (in practice, usually the address of the stream, but this
is not guaranteed). If you want to get the results of a call to
getlineinto anstd::ostringstream, you need two operations (with acheck for errors between them):
Similarly, to write the contents of a
std::ostringstream,is the correct solution. If you insist on using an
std::stringstreamwhen an
std::ostringstreamwould be more appropriate, you can also do:The first solution is preferable, however, as it is far more idiomatic.
In any case, once again, there is no
<<operator that takes anyiostreamtype, so you end up with the results of the implicitconversion to
boolor a pointer.