order of istream_iterator and cout statements

Discussion in 'C++' started by subramanian100in@yahoo.com, India, May 4, 2010.

  1. , India

    , India Guest

    I am using RedHat Enterprise Linux Workstation and g++ 3.4.3

    Consider the following program x.cpp:

    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include <iterator>

    using namespace std;

    int main()
    {
    string x("test string");
    cout << "x = " << x << endl;
    istream_iterator<string> isi(cin);
    cout << "string entered: " << isi->c_str() << endl;

    return EXIT_SUCCESS;
    }

    I compiled this program as
    g++ -std=c++98 -pedantic -Wall -Wextra x.cpp

    When I ran this program, it first printed
    x = test string
    then it waited for an input. For this, I entered
    sample

    Then it printed:
    string entered: sample

    Now consider the following program y.cpp:

    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include <iterator>

    using namespace std;

    int main()
    {
    string x("test string");
    istream_iterator<string> isi(cin);
    cout << "x = " << x << endl;
    cout << "string entered: " << isi->c_str() << endl;

    return EXIT_SUCCESS;
    }

    In this program, the istream_iterator and the first cout statements
    are interchanged.

    I compiled this program as
    g++ -std=c++98 -pedantic -Wall -Wextra y.cpp

    When I ran this program, it first waited for user input. For this I
    entered:
    sample

    It then printed:
    x = test string
    string entered: sample

    Why doesn't the second program y.cpp behave similar to the first
    program x.cpp ? For the second program also, I expected the following
    to be printed first:
    x = test string

    This should have been followed by user input and then finally the
    following should have been printed:

    string entered: whatever user gave as input

    But this doesn't happen with the second program y.cpp. What is the
    difference between the two programs ? I am unable to understand why
    the interchanging of the istream_iterator statement and first cout
    statement causes different program output.

    Kindly explain.

    Thanks
    V.Subramanian
    , India, May 4, 2010
    #1
    1. Advertising

  2. On 5/4/2010 8:26 AM, , India wrote:
    > [..] consider the following program y.cpp:
    >
    > #include<cstdlib>
    > #include<iostream>
    > #include<string>
    > #include<iterator>
    >
    > using namespace std;
    >
    > int main()
    > {
    > string x("test string");
    > istream_iterator<string> isi(cin);
    > cout<< "x = "<< x<< endl;
    > cout<< "string entered: "<< isi->c_str()<< endl;
    >
    > return EXIT_SUCCESS;
    > }
    >
    > In this program, the istream_iterator and the first cout statements
    > are interchanged.
    >
    > I compiled this program as
    > g++ -std=c++98 -pedantic -Wall -Wextra y.cpp
    >
    > When I ran this program, it first waited for user input. For this I
    > entered:
    > sample
    >
    > It then printed:
    > x = test string
    > string entered: sample
    >
    > Why doesn't the second program y.cpp behave similar to the first
    > program x.cpp ?


    For some reason I am thinking you're not looking for a simple answer
    like, "because they are two different programs"...

    > For the second program also, I expected the following
    > to be printed first:
    > x = test string


    Why? The 'istream_iterator' is defined like this:

    24.5.1 Class template istream_iterator [lib.istream.iterator]
    1 istream_iterator reads (using operator>>) successive elements
    from the input stream for which it was constructed. After it is
    constructed, and every time ++ is used, the iterator reads and
    stores a value of T.

    That means that it reads one value from the stream right after
    construction. You can think that the reading of one 'std::string' is
    done as the *last step of initializing* your stream with 'cin'.

    >
    > This should have been followed by user input


    "Should"? What makes you think that? What *source* do you use?

    > and then finally the
    > following should have been printed:
    >
    > string entered: whatever user gave as input
    >
    > But this doesn't happen with the second program y.cpp. What is the
    > difference between the two programs ?


    Uh... Hello? Can't you tell?

    > I am unable to understand why
    > the interchanging of the istream_iterator statement and first cout
    > statement causes different program output.


    RTFM, I guess.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, May 4, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. abi

    man cout or info cout

    abi, Jun 27, 2003, in forum: C++
    Replies:
    2
    Views:
    1,636
  2. Pmb

    std::cout vs cout

    Pmb, Jun 2, 2004, in forum: C++
    Replies:
    2
    Views:
    4,394
    Leor Zolman
    Jun 2, 2004
  3. Replies:
    4
    Views:
    309
    Howard
    Sep 15, 2005
  4. Stefan Ram

    Re: cout vs std::cout

    Stefan Ram, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    463
  5. Hendrik Schober

    Re: cout vs std::cout

    Hendrik Schober, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    426
    Jerry Coffin
    Oct 7, 2008
Loading...

Share This Page