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
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