Enhancement # 2 - Valid Input Range
To further enhance the asterisk triangle program the numeric input needs to be restricted to a certain value range. Such a valid range would be 1 through 79. Entering anything greater than 79 and all the visuals go out the window.
Replace the entire while loop with the following while snippet.
{
cout << "Must enter an integer (1 - 79): ";
cin.clear(); // clear cin error state, if exists
while (cin.get() != '\n') // clear out non newline characters
continue;
}
In reality, only the while statement needed to be replaced as the nothing drastically needed to be changed in the body of the loop. As you can see I did change the cout statement to better inform a user of what is required, a number between 1 - 79.
The while statement could have been written like so:
The || is the same as or and, though not used in this example, && is the same as and.
You will note that the program now detects incorrect data entry, that is any data entry commencing with a non numeric and any numeric entry not falling within the range of 1 through 79.
As the code stands now, what keyboard input doesn't it correctly trap? Entries such as '5prime' or '5.5' are not processed. Both of these entries are considered valid input as the first character is a numeric. Enter as input '80prime' or '80.5' and both are considered outside the valid value range. I'll discuss this in the next article.
- Printer-friendly version
- Login or register to post comments
- 199 reads


Recent comments
53 min 55 sec ago
1 day 41 min ago
1 day 6 hours ago
1 day 16 hours ago
1 day 17 hours ago
3 weeks 6 days ago
3 weeks 6 days ago
4 weeks 1 hour ago
10 weeks 1 day ago
11 weeks 4 days ago