We have learned how to input data from the keyboard to our

advertisement
Special Notes on Input Stream
Input Stream:
We have learned how to input data from the keyboard to our program in order to
manipulate them and output on the screen/file. Now we need to look into this process a
little bit deeper to understand how input stream, input stream variable cin, and the >>’s
(extraction operator) take parts in this process.
Input stream (denoted as istream) is a class declared in iostream header file. You don’t
have to understand the concept of class at this point. You will learn it in CS231. For now
let’s try to understand the concept of stream. cin is a input stream variable and we need to
understand “stream” in order to understand cin better.
A stream is a sequence of characters from the source (keyboard) to the destination (your
program). istream class (it is a program) is responsible to handle any kind of input that
come from a keyboard through an input stream. After characters come through the stream
they are kept in input stream variable called cin (same cin you use in your program).
From cin your program extracts these values by using an “>>” (extraction operator). The
extraction operator will keep extracting each value from the stream variable (cin) until it
encounters a white space or “\n” and load those values to a variable followed by it. White
space or “\n” is created by you when you press the enter key after entering input. Once it
encounters the white space it stops reading and leaves the marker to the beginning of that
white space.
Let’s take the following example in order to clarify this more:
string name;
int pay;
cout<<"Enter Income: ";
cin>>pay;
cout<<"Enter Name: ";
cin>>name;
cout<<"Income: "<<pay<<endl;
cout<<"Name: "<<name;
In the above example we see that the monitor displays the statement “Enter Income”.
Then the program encounters the istream variable cin. At this point your program is
expecting input in the stream from the keyboard. Once you enter input (let’s say 2000), it
travels from the keyboard through the stream as characters. istream class will deal with
this set of characters and load them to cin (input stream variable). Then your program
extracts (by using >> operator) these characters from cin and give to the variable which
follows it (in this case pay). So after this line of code is executed, the variable pay has
2000 in it.
Now what happens to the stream? Did all the characters get deleted after they were read
to pay? No. Those character values still exist in the input stream until the stream is full;
then they get dumped. Dumping is depended on your system’s settings.
So if the stream still has values how does the next set of characters get extracted? The
answer is there is a marker which keeps track of the characters in a stream. Every time
the extraction operator (>>) extracts a character, the marker is moved before the next
character. Then the extraction operator extracts that character and again marker is moved
before the next character. So as you see that the marker is the indication to the extraction
operator from where to begin the extraction. Now remember that the extraction operator
does not extract empty/white space from the stream that is created by you when you hit
enter on the keyboard (in this case after you entered 2000). The extraction operator will
extract 2000 and leave the marker before that empty/white space in the stream. So as you
see that the marker is the indication to the extraction operator from where to begin the
extraction and the white space (\n) is the indication to where to end.
So what happens when the next cin is executed by your program in line 4? After the
statement "name: " is displayed on the monitor, your program is expecting you to enter
name. After you enter a name the characters are read into the input stream variable (cin).
Then the extraction operator starts extracting. It avoids the white space created by you
after you pressed the enter key (after entering pay) and starts reading each character and
gives them to the variable called name.
The following figure will make this discussion clearer:
get/getline functions with Input Stream:
In the above discussion we learned that the >> operator avoids/ignores an empty space or
a white character during extraction created by you by hitting the enter key after entering
the input. So what about when you need to load your full name with spaces between your
first and last name? That’s when the function getline or get is used. In contrary to the
extraction operator, getline or get function both will extract and grab empty space and
leave the marker before the next stream character. Let’s take the following example:
cout<<"Enter Name: ";
cin>>name;
cout<<"Enter Income: ";
cin>>pay;
cout<<"Income is: "<<pay<<endl;
cout<<"Name is: "<<name;
cout<<"Please enter your full name:";
getline(cin,FullName);
cout<<"Please enter an angle in degrees:";
cin>>Angle;
In the above example, getline function will extract the white space (\n) created by you
after you entered pay and leave the marker before the next character to be extracted in the
stream variable (cin). So later when the angle value is input the extraction operator can
extract the characters and give them to the Angle variable. Now look at the following
example and try to understand what happens when the function getline is executed:
cout<< “Enter you monthly salary:”;
cin>>salary;
cout<<"Please enter your full name:";
getline(cin,FullName);
cout<<"Please enter an angle in degrees:";
cin>>Angle;
cout<< “Your full name is: ”<<FullName;
In the above example, user enters a figure for salary and the extraction operator extracts
those figure characters from cin and gives those characters to salary variable as integer
values. But the extraction operator leaves the marker before the white space (\n). Now
after user enters the name getline function begins to extract characters from the stream
variable cin. Problem is the marker is right before the white space left by the “>>”
operator and getline stops reading when it encounters a white space or “/n”. So getline
thinks that this is where the characters end for input stream and he grabs that white
character (\n) without taking any other character from the stream. So as a result the
characters you entered for name are left behind. So the statement “Your full name is: ”
only displays that white character grabbed by the getline function which is non printable.
As a result you do not see the name you entered. To solve this problem you need to use
another getline function or ignore function. Can you tell me how the following code
solves the problem?
cout<< “Enter you monthly salary:”;
cin>>salary;
cout<<"Please enter your full name:";
getline(cin,grab_WhiteSpace);
getline(cin,FullName);
cout<<"Please enter an angle in degrees:";
cin>>Angle;
cout<< “Your full name is: ”<<FullName;
Let’s look at the following code. What happens if the user mistakenly enters ABC instead
of entering only one character?
char ch;
cout<<"Enter a character"<<endl;
cin.get(ch);
cout<<"Character is "<<ch<<endl;
cout<<"Enter Another character: "<<endl;
cin.get(ch);
cout<<"Second Character is "<<ch<<endl;
Let’s look at the following code to make sure that all of the above discussion made sense
to you and to let you know that you can extract characters from other objects besides cin.
string mystr;
float price=0;
int quantity=0;
cout << "Enter price: ";
getline (cin,mystr);
stringstream(mystr) >> price;
cout<<"Price is: "<<price<<endl;
cin.ignore(10, '\n');
cout<<"mystr has: "<<mystr<<endl;
cout << "Enter quantity: ";
getline (cin,mystr);
cout<<"mystr has: "<<mystr<<endl;
stringstream(mystr) >> quantity;
cout<<"Quantity is: "<<quantity<<endl;
cout << "Total price: " << price*quantity << endl;
Download