ticky problem

Discussion in 'C++' started by onkar, May 31, 2008.

  1. onkar

    onkar Guest

    I am in a situation wherein I want to process input line by line .
    Each line contians numbers.

    sample input :

    1 2 3 4 5
    5 6 7
    1
    2 3 4 56 7 8
    0


    my program must take 1 2 3 4 5 first -> process it -> 5 6 7 -> process
    it and likewise. How can i do it without considering each line as a
    string and seperating out numbers ? Can I directly use some kind of
    loop ? and how is the loop going to terminate ?
     
    onkar, May 31, 2008
    #1
    1. Advertising

  2. onkar wrote:
    > I am in a situation wherein I want to process input line by line .
    > Each line contians numbers.
    >
    > sample input :
    >
    > 1 2 3 4 5
    > 5 6 7
    > 1
    > 2 3 4 56 7 8
    > 0
    >
    >
    > my program must take 1 2 3 4 5 first -> process it -> 5 6 7 -> process
    > it and likewise. How can i do it without considering each line as a
    > string and seperating out numbers ? Can I directly use some kind of
    > loop ? and how is the loop going to terminate ?


    The easiest way is probably read the input line by line with
    getline(), create an istringstream from that line, and then read the
    values with >> until there are no more.
     
    Juha Nieminen, May 31, 2008
    #2
    1. Advertising

  3. onkar wrote:

    > I am in a situation wherein I want to process input line by line .
    > Each line contians numbers.
    >
    > sample input :
    >
    > 1 2 3 4 5
    > 5 6 7
    > 1
    > 2 3 4 56 7 8
    > 0
    >
    >
    > my program must take 1 2 3 4 5 first -> process it -> 5 6 7 -> process
    > it and likewise. How can i do it without considering each line as a
    > string and seperating out numbers ? Can I directly use some kind of
    > loop ? and how is the loop going to terminate ?


    I recently had to do something like this. I decided to treat the input file
    as a set of logical records, rather than as a set of lines. A record can
    span multiple lines. Each record is terminated by a semicolon.

    I have a loop that uses the >> operator to read tokens into a string
    variable (called Token!). Each token string is then pushed into a list of
    strings (list<std:string> for further processing. A record is complete when
    I find a token that is (or ends with) a semicolon.

    The need for a semicolon terminator is necessary to give me the flexibility
    to have records spanning multiple lines.

    If you don't need this flexibility then the approach posted by juha Nieminem
    looks sensible to me.

    Chris Gordon-Smith
    www.simsoup.info
     
    Chris Gordon-Smith, May 31, 2008
    #3
  4. On 2008-05-31 18:58, Chris Gordon-Smith wrote:
    > onkar wrote:
    >
    >> I am in a situation wherein I want to process input line by line .
    >> Each line contians numbers.
    >>
    >> sample input :
    >>
    >> 1 2 3 4 5
    >> 5 6 7
    >> 1
    >> 2 3 4 56 7 8
    >> 0
    >>
    >>
    >> my program must take 1 2 3 4 5 first -> process it -> 5 6 7 -> process
    >> it and likewise. How can i do it without considering each line as a
    >> string and seperating out numbers ? Can I directly use some kind of
    >> loop ? and how is the loop going to terminate ?

    >
    > I recently had to do something like this. I decided to treat the input file
    > as a set of logical records, rather than as a set of lines. A record can
    > span multiple lines. Each record is terminated by a semicolon.
    >
    > I have a loop that uses the >> operator to read tokens into a string
    > variable (called Token!). Each token string is then pushed into a list of
    > strings (list<std:string> for further processing. A record is complete when
    > I find a token that is (or ends with) a semicolon.


    Could you not just have used getline(file, string, ';') instead of doing
    multiple >> operations?

    --
    Erik Wikström
     
    Erik Wikström, May 31, 2008
    #4
  5. Erik Wikström wrote:

    > On 2008-05-31 18:58, Chris Gordon-Smith wrote:
    >> onkar wrote:
    >>
    >>> I am in a situation wherein I want to process input line by line .
    >>> Each line contians numbers.
    >>>
    >>> sample input :
    >>>
    >>> 1 2 3 4 5
    >>> 5 6 7
    >>> 1
    >>> 2 3 4 56 7 8
    >>> 0
    >>>
    >>>
    >>> my program must take 1 2 3 4 5 first -> process it -> 5 6 7 -> process
    >>> it and likewise. How can i do it without considering each line as a
    >>> string and seperating out numbers ? Can I directly use some kind of
    >>> loop ? and how is the loop going to terminate ?

    >>
    >> I recently had to do something like this. I decided to treat the input
    >> file as a set of logical records, rather than as a set of lines. A record
    >> can span multiple lines. Each record is terminated by a semicolon.
    >>
    >> I have a loop that uses the >> operator to read tokens into a string
    >> variable (called Token!). Each token string is then pushed into a list of
    >> strings (list<std:string> for further processing. A record is complete
    >> when I find a token that is (or ends with) a semicolon.

    >
    > Could you not just have used getline(file, string, ';') instead of doing
    > multiple >> operations?
    >

    Possibly, although I would have still had to separate out the tokens.
    getline on its own would just give me a single string without separating
    out tokens.

    (If I had known about the alternate version of getline you quote I probably
    would indeed have used it!)

    Chris Gordon-Smith
    www.simsoup.info
     
    Chris Gordon-Smith, May 31, 2008
    #5
  6. On May 31, 8:26 pm, Chris Gordon-Smith <>
    wrote:
    > Erik Wikström wrote:
    > > On 2008-05-31 18:58, Chris Gordon-Smith wrote:
    > >> onkar wrote:

    >
    > >>> I am in a situation wherein I want to process  input line by line .
    > >>> Each line contians numbers.

    >
    > >>> sample input :

    >
    > >>> 1 2 3 4 5
    > >>> 5 6 7
    > >>> 1
    > >>> 2 3 4 56 7 8
    > >>> 0

    >
    > >>> my program must take 1 2 3 4 5 first -> process it -> 5 6 7 -> process
    > >>> it and likewise. How can i do it without considering each line as a
    > >>> string and seperating out numbers ? Can I directly use some kind of
    > >>> loop ? and how is the loop going to terminate ?

    >
    > >> I recently had to do something like this. I decided to treat the input
    > >> file as a set of logical records, rather than as a set of lines. A record
    > >> can span multiple lines. Each record is terminated by a semicolon.

    >
    > >> I have a loop that uses the >> operator to read tokens into a string
    > >> variable (called Token!). Each token string is then pushed into a list of
    > >> strings (list<std:string> for further processing. A record is complete
    > >> when I find a token that is (or ends with) a semicolon.

    >
    > > Could you not just have used getline(file, string, ';') instead of doing
    > > multiple >> operations?

    >
    > Possibly, although I would have still had to separate out the tokens.
    > getline on its own would just give me a single string without separating
    > out tokens.
    >
    > (If I had known about the alternate version of getline you quote I probably
    > would indeed have used it!)
    >
    > Chris Gordon-Smithwww.simsoup.info


    Can't you just:

    std::vector<int> numbers(0);
    std::vector<std::vector <int> > numberGroups(0);
    while(cin){
    while (cin.peek() != '\n'){
    int number;
    cin >> number;
    numbers.push_back(number);
    }
    numberGroups.push_back(numbers);
    //yourfunctionhere e.g. weirdfunction(numberGroups);
    numbers.clear();
    }

    Regards,

    Wanas
    http://programmingden.blogspot.com
     
    El-Hassan Wanas, Jun 1, 2008
    #6
    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. Andreas Suurkuusk
    Replies:
    0
    Views:
    4,030
    Andreas Suurkuusk
    Jul 27, 2003
  2. Ted Miller
    Replies:
    0
    Views:
    5,218
    Ted Miller
    Sep 13, 2003
  3. Merek
    Replies:
    0
    Views:
    2,000
    Merek
    Dec 3, 2003
  4. Scott Meddows
    Replies:
    1
    Views:
    421
    John Saunders
    Jun 8, 2004
  5. Mike

    Problem problem problem :( Need Help

    Mike, May 7, 2004, in forum: ASP General
    Replies:
    2
    Views:
    581
    Bullschmidt
    May 11, 2004
Loading...

Share This Page