order of istream_iterator and cout statements

  • Thread starter subramanian100in
  • Start date
S

subramanian100in

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
 
V

Victor Bazarov

[..] 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
 

Ask a Question

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top