get()
Enhancement # 3 - Trap number followed by non numeric
As you know, the Asterisk Triangle Program has one remaining flaw and it's a flaw that should be addressed.
Presently, if a user enters as input 50prime the number '50' is accepted as valid and the remaining 'prime' is ignored. If a user enters as input 50.5 the number '50' is accepted and the remaining '.5' is ignored. Ideally we need to trap this and force a true integer to be entered.
Replace this line of code
with this
You'll note that a third condition has been added to the while statement. That being cin.peek() != '\n'. What this is saying is,
"if the next character in the input buffer is not a newline character".
We know that after the execution of cin >> rows that if a true number was entered then all that is left in the input buffer is a newline ('\n') character. The peek() function merely checks for the next character in the input buffer. It doesn't remove the character from the input buffer. An entry such as '50prime' will cause the expression cin.peek() != '/n' to evaluate to true, as it detects the letter 'p', which in turn the body of the while loop is entered.
Here is the complete program code.
// 30 Apr, 2008.
#include <iostream>
Enhancement # 1 - Trap non numeric input
Okay, lets enhance the asterisk triangle program so that it only accepts numeric input. That is, if a user types in a non numeric then the program should re-request correct input until a number is entered.
Remove these lines:
cin.get();
and replace with:
{
cout << "Must enter an integer: ";
cin.clear(); // reset error state back to false
cin.get(); // flush out next character
}
An understanding of the cin object is required for this snippet. As the variable rows is defined as an integer type, attempting to assign a non integer to this type will set the cin objects error flag to true. The expression (cin >> rows) returns a boolean value (true or false) relating to the error state of the expression. We can include this error state as a conditional part of a while loop. while loops only continue whilst a condition is true and therefore in this instance we need to check for when not true which means false. Confusing, I know.
Compile and run the program. Enter a single non numeric character. You'll now note that the program asks you to input only an integer. Entering an integer is the only way to exit the loop and continue through to the end.
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.
cin.getline (arrayname, size);
So you want to read an entire line from the keyboard and not be bothered with cin >> getdata; only reading the first word. Then this form of the cin object is for you.
This form accepts an entire line entered from the keyboard terminated by the 'ENTER' key. The newline character created by the 'ENTER' key is replaced by a null '\0' character (which is the terminating character required for character arrays). As you can guess from the code snippet, the line of text is ultimately assigned to arrayname variable, a character array, and only receives the number of characters specified by the second parameter, size.
If the number of characters entered is greater than the number specified by the second parameter size the left-over characters remain in the input queue/stream. Remember, the next cin operation will retrieve either some or all of these characters depending upon the type of cin operation that's applied. If the number of characters is less than the parameter size then the input queue is effectively flushed and will not be a problem for the next cin operation.


Recent comments
2 hours 55 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