Thread: cin >> string // multiple lines

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    43

    cin >> string // multiple lines

    Hi,

    I have written a method that takes data for an std::string and processes it.
    I now (taking a step back) want to get this line from user. the string may have multiple line.. (meaning that i can't read only until '\n', and I have as certain const char* which is the way to stop the input.
    So, what would be a way to check when to stop adding the istream to std::string?

    Thanks....

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sure, there are several ways to do that. One would be to read a line at a time and just accumulate a long string from each line of text.

    Let's say we have a file that looks something like this:
    Code:
    Some text
    More text
    Another line of text
    :end
    You can then read a line at a time, and if it's not ":end", you continue with the next line.

    If the :end can be anywhere in the text, it gets a bit more complicated.

    For more help, it would be useful if you can describe the format in more detail.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    43
    ok. so here it is...
    I'm not reading from a file, rather i'm reciving input from user.
    I prefer to process the data only after i've collected all of it, so i can decide what allocations to make before I start working with, rather than allocation dynamicly after each entery.
    The input is in the form of:
    Code:
    <Song> <We Dont Need No Education 1> <125>
    <Song> <We Dont Need No Education 2> <78>
    <Song> <We Dont Need No Education 3> <366>
    <End song list>

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The same principle applies to both direct user input and file input.
    However, if the input is in a form where EACH line is a standalone complete packet of data - as you show it - then I would suggest a vector of std::string, and each line stored as a string. Seems much easier to deal with than to merge a bunch of strings together and then separating it out at the other end [sometimes that's what NEEDS to be done, but this doesn't seem like such a case].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    43
    being part of a school assignment, we are explicity forbidden to use STL...
    therefore, this is one of those cases....

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so make it an array of 100 arrays of char - if you get more than 100 lines of input from the user without <End song list>, process those 100 lines and then go back ask for more.

    Making a very long string will just make it complicated.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    43
    if you say so...
    thanks

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Of course, the other factor is "do you actually need to accumulate a set of strings, then process them" for a practical [or assignment] reason?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM