istringstream to bool

Discussion in 'C++' started by Agent Mulder, Aug 23, 2003.

  1. Agent Mulder

    Agent Mulder Guest

    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
     
    Agent Mulder, Aug 23, 2003
    #1
    1. Advertising

  2. "Agent Mulder" <> wrote in message
    news:bi7oot$r6p$1.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
     
    John Harrison, Aug 23, 2003
    #2
    1. Advertising

  3. Agent Mulder

    Chris Theis Guest

    "Agent Mulder" <> wrote in message
    news:bi7oot$r6p$1.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
     
    Chris Theis, Aug 23, 2003
    #3
  4. "Agent Mulder" <> wrote in message
    news:bi7oot$r6p$1.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
     
    Ivan Vecerina, Aug 23, 2003
    #4
  5. Agent Mulder

    Agent Mulder Guest

    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
     
    Agent Mulder, Aug 23, 2003
    #5
  6. "Agent Mulder" <> wrote in message
    news:bi7tfb$ar0$1.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
     
    Ivan Vecerina, Aug 23, 2003
    #6
  7. Agent Mulder

    White Wolf Guest

    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
     
    White Wolf, Aug 23, 2003
    #7
  8. 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.
     
    =?iso-8859-1?Q?Juli=E1n?= Albo, Aug 23, 2003
    #8
  9. Agent Mulder

    Agent Mulder Guest

    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
     
    Agent Mulder, Aug 24, 2003
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Samuele Armondi

    Allocating istringstream objects

    Samuele Armondi, Jul 11, 2003, in forum: C++
    Replies:
    1
    Views:
    371
    Samuele Armondi
    Jul 11, 2003
  2. Viet Le Hong

    istringstream question

    Viet Le Hong, Sep 18, 2003, in forum: C++
    Replies:
    3
    Views:
    675
    Buster Copley
    Sep 18, 2003
  3. bml

    istringstream???

    bml, Jan 27, 2004, in forum: C++
    Replies:
    3
    Views:
    1,059
    Karl Heinz Buchegger
    Jan 27, 2004
  4. Donald Canton

    istringstream syntax error question

    Donald Canton, Jan 29, 2004, in forum: C++
    Replies:
    1
    Views:
    409
    David Harmon
    Jan 30, 2004
  5. Luther Baker

    istringstream question

    Luther Baker, May 22, 2004, in forum: C++
    Replies:
    7
    Views:
    3,874
    John Harrison
    May 22, 2004
Loading...

Share This Page