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.
We're not done yet. There's still a problem or two. Enter as input a multiple non numeric characters, ie 'steve'. Rediculous, I know, as it's not in context but then are users ever in context. You'll note that the loop cycles five times, each time displaying the same message like so:
Must enter an integer: Must enter an integer: Must enter an
integer: Must enter an integer: Must enter an integer:
Not very pretty but this can be cleaned up. What happened? After hitting the 'ENTER' key inputting 'steve' each character was processed by the while loop as each character created an error state in the cin object.
Enter as input 'steve10'. The same effect as above but this time 10 rows are also printed. As soon as the while loop arrived at the digit one (1), it correctly established the integer number ten (10), which no longer did an error state exist and the rows were printed. This obviously is not a desired implementation. It can be fixed.
Replace the while loop with the following snippet.
{
cout << "Must enter an integer: ";
cin.clear(); // clear cin error state
while (cin.get() != '\n') // clear out non newline characters
continue;
}
As you can see we've added another while loop. This effectively removes all characters left in the input buffer, including numbers if present. At the end of this process the newline character still remains in the buffer. Remember though, that basic cin >> rows skips over newline characters, so is not an issue in our example.
As the code stands now, it still doesn't account for incorrect data entry as regards entries like '50prime'. The number 50 is considered valid input and the remaining 'prime' is ignored. Again, this should not be allowed. We'll address that component a little later.
- Printer-friendly version
- Login or register to post comments
- 286 reads


Recent comments
3 hours 40 min ago
1 day 3 hours ago
1 day 9 hours ago
1 day 19 hours ago
1 day 19 hours ago
3 weeks 6 days ago
3 weeks 6 days ago
4 weeks 4 hours ago
10 weeks 1 day ago
11 weeks 5 days ago