Computer Science Canada

How to read until a blank line?

Author:  SJ [ Sat Jun 28, 2008 4:02 pm ]
Post subject:  How to read until a blank line?

Hi I'm working on a spoj problem that requires reading in a series of inputs in cin until a blank line, for example,

1 2 3
4 5 6
10 20 30
*blank line here*

I'm noobishly new to c++ so I'm using cin>> to read in each number, but i don't know when to stop reading in. Is there a "blank line character"? When i program in java i would read in a string, check if it's null, if not then parse it - not sure if that's the way to go in c++. So, can someone give me suggestions?

Author:  btiffin [ Sat Jun 28, 2008 4:56 pm ]
Post subject:  RE:How to read until a blank line?

Hi,

Remove whitespace and newline, and if you have an empty string, you have a blank line. Or count them and compare to length; same effect, faster.

Or is you are using String, look through the STL or stdlib for a trim type operation or smart empty test.

Cheers

Author:  OneOffDriveByPoster [ Sat Jun 28, 2008 7:21 pm ]
Post subject:  Re: How to read until a blank line?

Random bits of a program that seems to do what you describe:
c++:
#include <iostream>
#include <sstream>
#include <string>

      getline(cin, s);
      istringstream ss(s);

Author:  SJ [ Mon Jun 30, 2008 10:14 am ]
Post subject:  RE:How to read until a blank line?

ahh ty guys, got it working Smile


: