View Single Post
Old 06-30-2005, 09:03 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Quote:
I'm just wondering how the program presented at the end of the tutorial knows when to stop accepting words from the standard input.
It reads till it reads EOS (End Of Stream) that beeing the end of file if it's a file you're reading from or a termination of your input stream when you read from stdin.
Quote:
It seems that I just don't know how to close the stream?
Terminate the tranmission, on *nix thats <ctrl>+D in DOS it's <ctrl>+S, it all depends on what system you're running.

A small example:
Code:
#include <iterator>
#include <vector>
#include <numeric>
#include <iostream>

using namespace std;

int main ()
{
  vector<int> d;
  int total = 0;
  //
  // Collect values from cin until end of file
  // Note use of default constructor to get ending iterator
  //
  cout << "Enter a sequence of integers (eof to quit): " ;
  copy(istream_iterator<int,char>(cin),
       istream_iterator<int,char>(),
       inserter(d,d.begin()));
  //
  // stream the whole vector and the sum to cout
  //
  copy(d.begin(),d.end()-1,
       ostream_iterator<int,char>(cout," + "));
  if (d.size())
    cout << *(d.end()-1) << " = " <<
      accumulate(d.begin(),d.end(),total) << endl;
  return 0;
}
Since this reads anything of type int then anything which isn't an int, like a char.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote