DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Simple question:string cin

  1. #1
    Brian Leung Guest

    Simple question:string cin


    Hi,

    this is a simple question, but what is the proper format to use cin.get,
    and cin.getline to input
    a line into a STL string?

    Thanks,

    Brian


  2. #2
    Dave Bradshaw Guest

    Re: Simple question:string cin


    Brian, istream::getline() reads data into a character array, not a std::string.
    However, there is a getline() for standard strings in <string>. Here's
    an example with cin:

    string s;
    while ( getline( cin, s ) )
    {
    // do something with s.
    }

    If you really want to use cin.getline() then you have to do it the hard way:

    char buf[256];
    while( cin.getline( buf, 256 ) )
    {
    string s = buf;
    // do something with s
    }

    There is no string oriented version of istream::get(), so you would have
    to do it the hard way. The syntax is similar to getline() or you can use
    the single char flavor: get(char& c). Keep in mind, get() will not remove
    the terminator from cin so you could hit an infinite loop. This is a good
    reason to prefer getline().

    "Brian Leung" <bleung@zoo.cam.ac.uk> wrote:
    >
    >Hi,
    >
    >this is a simple question, but what is the proper format to use cin.get,
    >and cin.getline to input
    >a line into a STL string?
    >
    >Thanks,
    >
    >Brian
    >



  3. #3
    a a Guest

    Re: Simple question:string cin


    open up "string" find the char * var they use for the string
    and write an inline operator char *( ) { return the_str; }
    or whatever the name of the string is, this however can cause
    major problems :(
    so there is really no other way, except write you own string
    class it wouldnt take but 3 days at the most.


    "Brian Leung" <bleung@zoo.cam.ac.uk> wrote:
    >
    >Hi,
    >
    >this is a simple question, but what is the proper format to use cin.get,
    >and cin.getline to input
    >a line into a STL string?
    >
    >Thanks,
    >
    >Brian
    >



  4. #4
    Brian Leung Guest

    Re: Simple question:string cin


    Thanks Dave,
    I think getline(cin,s) was what I was looking for.

    Following that question up, do I need to flush cin or something?
    What happens now is that I type some text in, and string s is OK.
    The next iteration of the loop, I type text in, and s is a
    null string,but the while loop still runs. When I hit return
    again, s is filled with what I typed in last, and the loop runs again.
    What have I done wrong?

    Cheers,

    Brian




    >Brian, istream::getline() reads data into a character array, not a std::string.
    > However, there is a getline() for standard strings in <string>. Here's
    >an example with cin:
    >
    >string s;
    >while ( getline( cin, s ) )
    >{
    > // do something with s.
    >}
    >
    >If you really want to use cin.getline() then you have to do it the hard

    way:
    >
    >char buf[256];
    >while( cin.getline( buf, 256 ) )
    >{
    > string s = buf;
    > // do something with s
    >}
    >
    >There is no string oriented version of istream::get(), so you would have
    >to do it the hard way. The syntax is similar to getline() or you can use
    >the single char flavor: get(char& c). Keep in mind, get() will not remove
    >the terminator from cin so you could hit an infinite loop. This is a good
    >reason to prefer getline().
    >
    >"Brian Leung" <bleung@zoo.cam.ac.uk> wrote:
    >>
    >>Hi,
    >>
    >>this is a simple question, but what is the proper format to use cin.get,
    >>and cin.getline to input
    >>a line into a STL string?
    >>
    >>Thanks,
    >>
    >>Brian
    >>

    >



  5. #5
    Dave Bradshaw Guest

    Re: Simple question:string cin


    I don't think that you've done anything wrong. I'm guessing you are using
    MSVC and there is a known bug in the string getline function. The good news
    is that you can fix it with a 1 line change to the system header file string.
    The bug report can be found at:

    http://support.microsoft.com/support.../Q240/0/15.ASP

    "Brian Leung" <bleung@zoo.cam.ac.uk> wrote:
    >
    >Thanks Dave,
    >I think getline(cin,s) was what I was looking for.
    >
    >Following that question up, do I need to flush cin or something?
    >What happens now is that I type some text in, and string s is OK.
    >The next iteration of the loop, I type text in, and s is a
    >null string,but the while loop still runs. When I hit return
    >again, s is filled with what I typed in last, and the loop runs again.
    >What have I done wrong?
    >
    >Cheers,
    >
    >Brian
    >
    >
    >
    >
    >>Brian, istream::getline() reads data into a character array, not a std::string.
    >> However, there is a getline() for standard strings in <string>. Here's
    >>an example with cin:
    >>
    >>string s;
    >>while ( getline( cin, s ) )
    >>{
    >> // do something with s.
    >>}
    >>
    >>If you really want to use cin.getline() then you have to do it the hard

    >way:
    >>
    >>char buf[256];
    >>while( cin.getline( buf, 256 ) )
    >>{
    >> string s = buf;
    >> // do something with s
    >>}
    >>
    >>There is no string oriented version of istream::get(), so you would have
    >>to do it the hard way. The syntax is similar to getline() or you can use
    >>the single char flavor: get(char& c). Keep in mind, get() will not remove
    >>the terminator from cin so you could hit an infinite loop. This is a good
    >>reason to prefer getline().
    >>
    >>"Brian Leung" <bleung@zoo.cam.ac.uk> wrote:
    >>>
    >>>Hi,
    >>>
    >>>this is a simple question, but what is the proper format to use cin.get,
    >>>and cin.getline to input
    >>>a line into a STL string?
    >>>
    >>>Thanks,
    >>>
    >>>Brian
    >>>

    >>

    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center