6 Replies - 633 Views - Last Post: 19 July 2010 - 01:04 AM Rate Topic: -----

#1 coconut  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 10-February 10

End of stream

Posted 18 July 2010 - 02:51 PM

Hi, I have a piece of code that reads from the cin stream and does some operations with the stream. Everything is working fine, except because I don't know how many lines the input is going to have and I am looking for a way to tell my code to keep reading new input and doing these operations until the end of the stream.

I'm pretty sure this is an easy issue, but I haven't been able to fix it! I'm trying to use a while loop like this:

while (std::ios::eofbit){
//Operations
}



But I must be forgetting something because the loop never ends :-/

Thanks!

This post has been edited by coconut: 18 July 2010 - 02:52 PM


Is This A Good Question/Topic? 0
  • +

Replies To: End of stream

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1431
  • View blog
  • Posts: 4,963
  • Joined: 19-February 09

Re: End of stream

Posted 18 July 2010 - 03:17 PM

std::ios::eofbit is like a bitmask, so the while condition will always be true.

Could use eof(), good() is better. It tests for eof() as well as failed stream read.

// is = name of input stream
while (is.good()){
//Operations
}


Was This Post Helpful? 0
  • +
  • -

#3 coconut  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 10-February 10

Re: End of stream

Posted 18 July 2010 - 03:44 PM

Thanks! That worked but now it goes through the loop twice with the last set of inputs. Ie: if I was trying to get two numbers each time to sum them up and my input was:

2 1
3 5

The output is:

2 1 3
3 5 8
3 5 8

It's as if it took it one iteration too many :-/ Ideas? Thanks again!
Was This Post Helpful? 0
  • +
  • -

#4 Guest_c.user*


Reputation:

Re: End of stream

Posted 18 July 2010 - 06:34 PM

    int a, b;

    while (cin >> a && cin >> B)/>
        cout << a
             << " "
             << b
             << " "
             << a + b
             << endl;




also
while (cin.getline(...)) or while (getline(...))
and then, inside the loop, make a stringstream for the inputed line

cin.getline() - is for char[]
getline() - is for string

don't look at the big B, it's an old problem

This post has been edited by c.user: 18 July 2010 - 06:44 PM

Was This Post Helpful? 0

#5 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: End of stream

Posted 18 July 2010 - 07:03 PM

Why hello ladies, is your problem fully resolved?
Was This Post Helpful? 0
  • +
  • -

#6 coconut  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 10-February 10

Re: End of stream

Posted 19 July 2010 - 12:51 AM

Yes! That worked perfect! Thanks!
Was This Post Helpful? 0
  • +
  • -

#7 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: End of stream

Posted 19 July 2010 - 01:04 AM

Glad I could be of assistance.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1