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
- Add new comment
- 485 reads


Recent comments
38 weeks 1 day ago
39 weeks 2 days ago
40 weeks 1 day ago
40 weeks 2 days ago
49 weeks 4 days ago
1 year 12 weeks ago
1 year 12 weeks ago
1 year 14 weeks ago
1 year 14 weeks ago
1 year 14 weeks ago