-1

At first sorry about my English. Im preparing for my exam and I need to practice iterators, that dont work with containers. I would like to input some symbols right to the symbol like '\n' and I have no clue how can I do that. The problem is I am not able to stop entering symbols. Hope you can help me. Sincerely thank you!


#include <iostream>
#include <iterator>
#include <algorithm>
#include <fstream>
using namespace std;
int main()
{
    ofstream out("name.txt");
    istream_iterator<char> it(cin);
    istream_iterator<char> end_of_stream;
    ostream_iterator<char> fout(out, " ");
    copy (it, end_of_stream, fout);
}

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • You are iterating until "end of stream" so you must provide it. Usually you can use to do so. https://unix.stackexchange.com/questions/110240/why-does-ctrl-d-eof-exit-the-shell – Simon Kraemer May 29 '20 at 09:40
  • @SimonKraemer The point is that the OP does not want to iterate to end of stream. `I would like to input some symbols right to the symbol like '\n'` – john May 29 '20 at 10:12
  • @john `The problem is I am not able to stop entering symbols.` But maybe I misinterpreted the question. There's a reason I just wrote a comment and not an answer. – Simon Kraemer May 29 '20 at 10:15
  • @SimonKraemer Fair enough. I only made the comment because the question got closed as a dup on the same interpretation as you made. – john May 29 '20 at 10:45
  • thank you guys, you both helped me so much! – Yura Kanafotskiy May 29 '20 at 10:46

1 Answers1

0

There's no algorithm to copy until you get a particular value (I think) so instead you have to write your own loop.

Like this

while (it != end_of_stream && *it != '\n')
{
    fout << *it;
    ++it;
}
john
  • 85,011
  • 4
  • 57
  • 81