473,322 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

istringstream to bool

I try to remove the spaces from a string
using an old trick that involves an
istringstream object. I expect the while-condition
while(istringstream>>string) to evaluate to
false once the istringstream is exhausted, but
that is not the case. What am I missing?

#include<iostream>
#include<sstream>
#include<string>
static std::string s="Asymmetric Beards, \
Not Even Punk Rockers Have One!";
int main(int,char**)
{
std::istringstream a(s);
std::string b;
while(a>>b)std::cout<<b.c_str();
return 0;
}
.......
output
AsymmetricBeards,NotEvenPunkRockersHaveOne!One!One !One!One!One!One!...

-X
Jul 19 '05 #1
8 5323

"Agent Mulder" <mb*******************@home.nl> wrote in message
news:bi**********@news3.tilbu1.nb.home.nl...
I try to remove the spaces from a string
using an old trick that involves an
istringstream object. I expect the while-condition
while(istringstream>>string) to evaluate to
false once the istringstream is exhausted, but
that is not the case. What am I missing?

#include<iostream>
#include<sstream>
#include<string>
static std::string s="Asymmetric Beards, \
Not Even Punk Rockers Have One!";
int main(int,char**)
{
std::istringstream a(s);
std::string b;
while(a>>b)std::cout<<b.c_str();
return 0;
}
......
output
AsymmetricBeards,NotEvenPunkRockersHaveOne!One!One !One!One!One!One!...

-X


Works for me, maybe your STL is bugged?

john
Jul 19 '05 #2
"Agent Mulder" <mb*******************@home.nl> wrote in message
news:bi**********@news3.tilbu1.nb.home.nl...
I try to remove the spaces from a string
using an old trick that involves an
istringstream object. I expect the while-condition
while(istringstream>>string) to evaluate to
false once the istringstream is exhausted, but
that is not the case. What am I missing? .... std::istringstream a(s);
std::string b;
while(a>>b)std::cout<<b.c_str();


I believe this loop shall end once EOF is reached.
And it does on the compiler I tested.
Might be a compiler/library bug?
Maybe try testing for the end condition explicitly:
while( a>>b && a.good() )

You may also want to step through the library's source
code to see what is happening...

hth - Ivan
--
http://www.post1.com/~ivec
Jul 19 '05 #3

"Agent Mulder" <mb*******************@home.nl> wrote in message
news:bi**********@news3.tilbu1.nb.home.nl...
I try to remove the spaces from a string
using an old trick that involves an
istringstream object. I expect the while-condition
while(istringstream>>string) to evaluate to
false once the istringstream is exhausted, but
that is not the case. What am I missing?

[SNIP]

What compiler are you using? The code works well with VC++ 6.0 (SP5) and gcc
2.96. I guess it might be a compiler problem because the following variation
will work on VC++ but not with gcc 2.96. Using gnu you'll get the last word
twice.

while( a ) {
a >> b;
cout << b;
}

Regards
Chris
Jul 19 '05 #4
AM >
#include<iostream>
#include<sstream>
#include<string>
static std::string s="Asymmetric Beards, \
Not Even Punk Rockers Have One!";
int main(int,char**)
{
std::istringstream a(s);
std::string b;
while(a>>b)std::cout<<b.c_str();
return 0;
}
.......
output
AsymmetricBeards,NotEvenPunkRockersHaveOne!One!One !One!One!One!One!...

CT> What compiler are you using? The code works well with VC++ 6.0 (SP5)
and gcc
CT> 2.96.

I use Borland 5.5.1. Note also that I use string.c_str() to
output the string with cout. Leaving c_str() out crashes
the computer. Compiler bogus?

-X
Jul 19 '05 #5

"Agent Mulder" <mb*******************@home.nl> wrote in message
news:bi**********@news3.tilbu1.nb.home.nl...
CT> What compiler are you using? The code works well with VC++ 6.0 (SP5)
and gcc
CT> 2.96.

I use Borland 5.5.1. Note also that I use string.c_str() to
output the string with cout. Leaving c_str() out crashes
the computer. Compiler bogus?


Compiler, or (maybe more likely) library implementation.
I would recommend asking about the issue on a forum dedicated
to the platform you use.

Alternatively, you could try using another C++ library implementation
with your compiler. Such as the free STLport (http://www.stlport.org/).

hth,
Ivan
--
http://www.post1.com/~ivec
Jul 19 '05 #6
Agent Mulder wrote:
[SNIP]
I use Borland 5.5.1. Note also that I use string.c_str() to
output the string with cout. Leaving c_str() out crashes
the computer. Compiler bogus?


Leaving out c_str should not crash anything. I was using the Borland free
command line (I guess the version you have said is around that) and these
things worked for me. Sorry for asking, but is this for real, or you just
wanted another chance to fire your beard joke?

--
WW aka Attila
Jul 19 '05 #7
Agent Mulder escribió:
#include<iostream>
#include<sstream>
#include<string>
static std::string s="Asymmetric Beards, \
Not Even Punk Rockers Have One!";
int main(int,char**)
{
std::istringstream a(s);
std::string b;
while(a>>b)std::cout<<b.c_str();
return 0;
}
......
output
AsymmetricBeards,NotEvenPunkRockersHaveOne!One!One !One!One!One!One!...


If the while condition is ever true, when the ouput is done?

Regards.
Jul 19 '05 #8
WW> Sorry for asking, but is this for real, or you just
WW> wanted another chance to fire your beard joke?

(-: It's for real. True, I do think that asymmetric beard business is
very funny, I read it first in a Larry Niven book, where the character was
overly concerned about his asymmetric beard. In a C++
context, I think it represents the two files needed to represent
one class. The .h file and the .cpp file are asymmetric but make
up one 'entity'. In a beard context, it means that it is diffucult to
wash and comb an asymmetric beard, let alone spray day-glow
glitters in it (maintaince). There is a website dedicated at the
asymmetric beard at:

http://www.halfbakery.com/idea/asymmetric_20beards
Jul 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Samuele Armondi | last post by:
Hi everyone, Since istringstream objects are not assignable, I'm using the following code to allocate some dynamically. My question is: Is this the correct way of doing it? Am I deleting all the...
3
by: bml | last post by:
Could you help and answer my questions of istringstream? Thanks a lot! 1. Reuse an "istringstream" istringstream ist; ist.str("This is FIRST test string"); ist.str("This is SECOND test...
1
by: Donald Canton | last post by:
Hi, I'm using Bjarne's book to learn C++ and am stuck on the Calc program in Section 6. Everything works fine except when I try to use istringstream to parse a token from the command line. I...
7
by: Luther Baker | last post by:
Hi, My question is regarding std::istringstream. I am serializing data to an ostringstream and the resulting buffer turns out just fine. But, when I try the reverse, when the istringstream...
4
by: dinks | last post by:
Hi I'm really new to c++ so please forgive me if this is really basic but im stuck... I am trying to make a data class that uses istringstram and overloaded << and >> operators to input and output...
6
by: JustSomeGuy | last post by:
I am passing an istringstream to a function. I want that function to get a copy of the istringstream and not a refrence to it. ie when the function returns I want the istringstream to be...
8
by: Randy Yates | last post by:
Why does this: string AWord(string& line) { return line; } bool MYOBJECT::MyFunction(string &line) { int day;
6
by: James Aguilar | last post by:
Hello all, I am trying to use an istringstream to do some input off of cin by lines. The following snippet does not work: char buf; cin.getline(buf, 90); istringstream line1(string(buf));
11
by: icanoop | last post by:
I would like to do this MyClass x; istringstream("XXX") >> x; // Works in VC++ but not GCC instead of MyClass x; istringstream iss("XXX"); iss >> x; // Works in both GCC and VC++
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.