Is there something wrong with this?

A

Adrian

When I try this on MS C++2005 it takes the bad stream branch, and I
have no idea why. Borland and Gcc both do what I would expect.

Have I done something stupid?

#include <iostream>
#include <string>
#include <sstream>

int main()
{
int x=0;
std::string s("17,17,0,C,TOR,129/143");

std::istringstream strm(s);

strm >> x;

if(strm.good())
{
std::cout << x;
}
else
{
std::cout << "bad stream";
}

return 0;
}
 
A

Alf P. Steinbach

* Adrian:
When I try this on MS C++2005 it takes the bad stream branch, and I have
no idea why. Borland and Gcc both do what I would expect.

Have I done something stupid?

#include <iostream>
#include <string>
#include <sstream>

int main()
{
int x=0;
std::string s("17,17,0,C,TOR,129/143");

std::istringstream strm(s);

strm >> x;

if(strm.good())
{
std::cout << x;
}
else
{
std::cout << "bad stream";
}

return 0;
}

good() is not the opposite of fail().

Yeah, it's stupid.

But that's the way it is.
 
N

Noah Roberts

Alf said:
* Adrian:

good() is not the opposite of fail().

Yeah, it's stupid.

But that's the way it is

According to Josutis goodbit means that all the rest are off. That
means one of eofbit, failbit, or badbit must be set. It seems to me
that none of those apply; the value of x should be 17 and the next
character in the stream should be ',' no?
 
N

Nate Silva

Adrian said:
int x=0;
std::string s("17,17,0,C,TOR,129/143");

std::istringstream strm(s);

strm >> x;

Microsoft C++ thinks the comma is a thousands separator.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top