Data delimited by space key
I've been assigned a开发者_Go百科 problem I simply do not understand. I know that I need to use a cin function (like cin.get()), but I'm not sure which one I need or how to use it in this circumstance.
I need to create an insertion and extraction operator that reads (and writes) 3 pieces of data. All of the data is of the type int. For context, the data is the whole part of a fraction, the numerator, and the denominator. The data needs to be delimited by spaces, and the operators will be used for file input and output.
What I really want to know is which cin function I should use, and what the particular syntax should be considering I want to store the value in an integer.
Thanks in advance!
With cin
, you can just read data using something like this:
int wholepart,numer,denom;
cin>>wholepart>>numer>>denom;
This would read 3 integers into wholepart
, numer
and denom
respectively. It will skip over whitespace separating the integers.
The normal operator>>
for int
s expects the data to be separated by whitespace, so you should be able to just use it and interpret the results as you see fit.
精彩评论