Reading Character Data Using get
- You can also read characters using the get method (function) of cin
- get will not skip white space; it will store the white space character into the destination variable
- This is useful if you really want every key that the user pressed
Example contrasting both methods. Assume the user types: A B C <Enter>
Using the extraction operator will cause ch1, ch2, and ch3 to have the values 'A', 'B', and 'C', respectively.
char ch1, ch2, ch3;
cin >> ch1 >> ch2 >> ch3;
If we use get instead, ch1, ch2, ch3 will have the values 'A', ' ' (space), and 'B', respectively.
cin.get(ch1);
cin.get(ch2);
cin.get(ch3);
Next page
Previous page
Go to Lesson 4 index.