function to read a stream

Discussion in 'C++' started by nvangogh, May 13, 2014.

  1. nvangogh

    nvangogh Guest

    Hi, I am stuck on exercise 8.1 (at section 8.1.2) p314 of C++ Primer 5th
    Edition (Lippman, Lajoie, Moo). The exercise says:

    "Write a function that takes and returns an istream&. The function
    should read the stream until it hits end-of-file. The function should
    print what it reads to the standard output. Reset the stream so that it
    is valid before returning the stream."

    Suppose I have declared:

    istream& read(istream&);

    How do I go about defining this function?
    Thanks in advance.
     
    nvangogh, May 13, 2014
    #1
    1. Advertisements

  2. On 5/13/2014 12:13 PM, nvangogh wrote:
    > Hi, I am stuck on exercise 8.1 (at section 8.1.2) p314 of C++ Primer 5th
    > Edition (Lippman, Lajoie, Moo). The exercise says:
    >
    > "Write a function that takes and returns an istream&. The function
    > should read the stream until it hits end-of-file. The function should
    > print what it reads to the standard output. Reset the stream so that it
    > is valid before returning the stream."
    >
    > Suppose I have declared:
    >
    > istream& read(istream&);
    >
    > How do I go about defining this function?


    Start by giving the name to the argument, opening the body of the
    function (a curly brace should do), and returning the argument passed
    in, then closing the body (another curly brace).

    Do you know how to read the stream? The simplest is reading one char at
    a time. Do you know how to check the stream's "end-of-file" condition?
    Do you know how to define a loop *until* some condition is met (hint:
    use 'while')?

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, May 13, 2014
    #2
    1. Advertisements

  3. nvangogh

    nvangogh Guest

    On 13/05/14 17:19, Victor Bazarov wrote:
    > On 5/13/2014 12:13 PM, nvangogh wrote:
    >> Hi, I am stuck on exercise 8.1 (at section 8.1.2) p314 of C++ Primer 5th
    >> Edition (Lippman, Lajoie, Moo). The exercise says:
    >>
    >> "Write a function that takes and returns an istream&. The function
    >> should read the stream until it hits end-of-file. The function should
    >> print what it reads to the standard output. Reset the stream so that it
    >> is valid before returning the stream."
    >>
    >> Suppose I have declared:
    >>
    >> istream& read(istream&);
    >>
    >> How do I go about defining this function?

    >
    > Start by giving the name to the argument, opening the body of the
    > function (a curly brace should do), and returning the argument passed
    > in, then closing the body (another curly brace).
    >
    > Do you know how to read the stream? The simplest is reading one char at
    > a time. Do you know how to check the stream's "end-of-file" condition?
    > Do you know how to define a loop *until* some condition is met (hint:
    > use 'while')?
    >
    > V

    could you show the code?
     
    nvangogh, May 13, 2014
    #3
  4. nvangogh

    nvangogh Guest

    Is this a correct solution?


    #include <iostream>
    #include <string>
    istream& read(istream& in)
    {
    std::string word;
    while(in >> word)
    {
    std::cout << word;
    }

    // reset stream so it's in a valid state
    in.clear();
    return in;
    }
     
    nvangogh, May 13, 2014
    #4
  5. nvangogh

    nvangogh Guest

    If I try to compile this I get an error:

    #include <iostream>
    #include <string>

    // declaration
    istream& read(istream&);

    // definition
    istream& read(istream& in)
    {
    std::string word;
    while(in >> word)
    {
    std::cout << word << endl;
    }

    // reset stream so it's in a valid state
    in.clear();
    return in;
    }

    int main()
    {
    std::cout << "Input some txt";
    read(std::cin);
    return 0;
    }

    ------------------------
    > g++ -std=c++11 -o exercise8.1 exercise8.1.cpp
    > exercise8.1.cpp:7:1: error: ‘istream’ does not name a type
    > exercise8.1.cpp:10:1: error: ‘istream’ does not name a type
    > exercise8.1.cpp: In function ‘int main()’:
    > exercise8.1.cpp:26:16: error: ‘read’ was not declared in this scope

    -------------------------

    Can someone please show me how to do this exercise?
     
    nvangogh, May 13, 2014
    #5
  6. nvangogh

    nvangogh Guest

    On 13/05/14 18:42, nvangogh wrote:
    > If I try to compile this I get an error:
    >
    > #include <iostream>
    > #include <string>
    >
    > // declaration
    > istream& read(istream&);
    >
    > // definition
    > istream& read(istream& in)
    > {
    > std::string word;
    > while(in >> word)
    > {
    > std::cout << word << endl;
    > }
    >
    > // reset stream so it's in a valid state
    > in.clear();
    > return in;
    > }
    >
    > int main()
    > {
    > std::cout << "Input some txt";
    > read(std::cin);
    > return 0;
    > }
    >
    > ------------------------
    >> g++ -std=c++11 -o exercise8.1 exercise8.1.cpp
    >> exercise8.1.cpp:7:1: error: ‘istream’ does not name a type
    >> exercise8.1.cpp:10:1: error: ‘istream’ does not name a type
    >> exercise8.1.cpp: In function ‘int main()’:
    >> exercise8.1.cpp:26:16: error: ‘read’ was not declared in this scope

    > -------------------------
    >
    > Can someone please show me how to do this exercise?
    >
    >

    Its ok I've done it
     
    nvangogh, May 13, 2014
    #6
  7. nvangogh

    nvangogh Guest

    On 13/05/14 18:31, James Moe wrote:
    > On 05/13/2014 10:28 AM, nvangogh wrote:
    >>> Do you know how to read the stream? The simplest is reading one char at
    >>>> a time. Do you know how to check the stream's "end-of-file" condition?
    >>>> Do you know how to define a loop *until* some condition is met (hint:
    >>>> use 'while')?
    >>>>

    >> could you show the code?
    >>

    > How did you get to exercise 8.1 without knowing any of the above?
    >

    I know its a simple exercise, which I have solved now.
     
    nvangogh, May 13, 2014
    #7
    1. Advertisements

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. Rasmusson, Lars
    Replies:
    1
    Views:
    1,080
    popov
    Apr 30, 2004
  2. Replies:
    9
    Views:
    1,005
    Alex Buell
    Apr 27, 2006
  3. Alexander Korsunsky

    get stream mode flags from an opened stream

    Alexander Korsunsky, Feb 17, 2007, in forum: C++
    Replies:
    1
    Views:
    725
    John Harrison
    Feb 17, 2007
  4. dolphin
    Replies:
    6
    Views:
    829
    Thomas Fritsch
    Mar 18, 2007
  5. mrstephengross
    Replies:
    3
    Views:
    643
    James Kanze
    May 10, 2007
  6. Kashif Ur Rehman
    Replies:
    2
    Views:
    1,135
    Tom Hawtin
    May 17, 2007
  7. Anand Hariharan

    Why no diagnostic for stream << stream?

    Anand Hariharan, Mar 17, 2009, in forum: C++
    Replies:
    2
    Views:
    438
    Anand Hariharan
    Mar 18, 2009
  8. Roedy Green

    Byte Stream Vs Char Stream Buffer

    Roedy Green, May 7, 2014, in forum: Java
    Replies:
    21
    Views:
    989
    cronynaval
    Jun 13, 2017
Loading...