473,406 Members | 2,619 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,406 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 5330

"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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.