On page 78 of the tutorial (that is pg 78 of the pdf version) a structure is created with an instance of this structure being called yours, and one of its members being int year. He gets this value from the user and stuffs it into the structure with the following two lines of code:
What is going on here? Why can't the year value be gotten from the user as an int and just stuffed directly into the sturcture? Was this stringstream function even discussed in the tutorial before this point?
Why can't the year value be gotten from the user as an int and just stuffed directly into the sturcture?
There are several problems with using std::cin. Suffice it to say that you should never use it.
That's not quite a function call. There is a function call going on there, but there's more.
That's really a constructor call. One of the std::stringstream constructors takes an std::string and feeds it to the stream. operator>>() then converts the contents of the stream into an integer and assigns it to yours.year.
Thanks it is now at least making some sort of sense to me. That article does it a little different from the tutorial. To take in an integer value from the user the article has the following code:
line 9 string input = "";
line 21 getline(cin, input);
line 24 stringstream myStream(input);
which seems to have the effect of placing the input into an int variable called myStream; it seems to have this effect because on the next line it compares myStream with the int myNumber, however I can't see where myStream is ever declared. Actually the code is
line 25 if (myStream >> myNumber)
so actually I don't know what it is doing since I have never seen >> used in comparison before.
myStream is not an int, it is a stringstream
if you look line 25 carefully you will see this: if (myStream >> myNumber) which isn't comparing myStream to myNumber but it is using the >> operator ( which is the same operator of cin >> myNumber ) and checking if the extraction from myStream was successfull.
Your tutorial skips the checking and it doesn't declare a named variable but it uses the stringstream constructor.
The line stringstream(mystr) >> yours.year;
Is the same as