cin.get (arrayname, size)
Here is another form of the cin use. This form, cin.get(arrayname, size) is similar to cin.getline(arrayname, size) discussed previously. This form cin.get(arrayname, size) reads an entire line from the keyboard terminated by the 'ENTER' key BUT leaves the newline '\n' character in the input queue. Why it does this I'm not sure and why have such a function which for all intents and purposes is the same as the cin.getline(...) method, I'm not sure either.
Some things to be aware of
If there is an existing newline in the input buffer prior to calling cin.get(arrayname,20);, this form will not accept it, remember it leaves the newline character in the input buffer. The point here is that an error state is created in the cin object; the failbit flag is set to true. No further cin operations are permitted whilst the error flag is set to true. In order to remove the error flag this command cin.clear(); is required. It resets the cin error state back to false.
The above is not applicable if the input buffer contains normal characters ending with a newline character prior to calling the cin.get(arrayname, 20);. What occurs in this instance is that the errant characters or characters already in the input buffer as a result of a previous cin operation are assigned or allocated to the character array arrayname. The newline character is left in the input buffer.
Another situation arises whereby the user types in more characters than is specified by the size parameter of the cin function. No error state is recorded and the character array arrayname only receives the number of specified characters. The remaining characters are left in the input buffer.
The following code snippet should explain the various workings of this topic. The code, as is, will create an error state, simply uncomment the line after cin.get(line, 20);.
// Author : Steven Taylor
// Date : 23 Apr 2008
#include <iostream>
using namespace std;
int main()
{
char getdata;
cout << "Enter one character: ";
cin >> getdata;
cin.get(); // clear out very next character (newline)
char line[20];
cout << "\nEnter a line of text 20 characters max: ";
cin.get(line,20);
cout << "\nLine of text entered : " << line;
//cin.get(); // flush out the newline character
char line2[20];
cout << "\nEnter another line of text 20 characters max: ";
cin.get(line2,20);
cout << "\nSecond line of text entered : " << line2;
cout << "\n\nNow at the end of the program" << endl;
cout << "First character of line[20] : " << line[0] << " character code is : " << (int)line[0] << endl;
cout << "First character of line2[20] : " << line2[0] << " character code is : " << (int)line2[0]<< endl;
if (cin.eof())
cout << "\n cin.eof() == true";
else
cout << "\n cin.eof() == false";
if (cin.fail())
cout << "\n cin.fail() == true";
else
cout << "\n cin.fail() == false";
// exit routine
cin.clear(); // only use this if there is potential for an error state.
while (cin.get() != '\n')
continue;
cin.get();
return 0;
}
As an aside, if there is no error state, issuing the command
cin.clear(); has no side effects, that is, errors are not reported simply because no error exists to start with. It's harmless.
Play with the program. You will note that if you input more than a single character for the first input, that it adversely affects the following cin methods. I'll deal with flushing the input stream/buffer/queue in a separate article but in the interim the exit routine should be a clue as to how to flush the input buffer.
- Printer-friendly version
- Login or register to post comments
- 234 reads


Recent comments
2 hours 46 min ago
1 day 2 hours ago
1 day 8 hours ago
1 day 18 hours ago
1 day 19 hours ago
3 weeks 6 days ago
3 weeks 6 days ago
4 weeks 3 hours ago
10 weeks 1 day ago
11 weeks 5 days ago