Prime 357

We'll learn something

Site Menu

  • Home
  • Recent Posts
  • Forum
    • Programming Languages
      • C++
    • Website Design & Content Management
      • Wordpress >> Drupal
  • Blogs
  • Topics
    • C++
    • Changing hosts - Dummies Guide
    • Wordpress >> Drupal
  • Download Centre
  • Contact us
Home C++ (The Book) Short Programs Create a triangle type pattern


Image - OpenID

User login

What is OpenID?
  • Log in using OpenID
  • Cancel OpenID login
  • Create new account
  • Request new password

Navigation

  • Recent posts

Topics

  • C++ (The Book)
    • Basic cin operations
    • Short Programs
      • Create a triangle type pattern
        • Enhancement # 1 - Trap non numeric input
        • Enhancement # 2 - Valid Input Range
        • Enhancement # 3 - Trap number followed by non numeric
      • Array Solution - Half and half, 10 per line
      • Calculate Pace (Running Program)
      • Calculate Pace (Running Program) - # 2
      • Remove Vowels
      • Remove Vowels - # 2 - String version
    • Compiler/Linker Error Messages
  • Changing Hosts - a Dummies Guide
  • Wordpress to Drupal

Recent comments

  • Thanks..
    37 weeks 1 day ago
  • Hmmm,Interesting one,thx for
    38 weeks 2 days ago
  • Buyer beware
    39 weeks 1 day ago
  • REPLY:Actual Processes Involved
    39 weeks 2 days ago
  • Back to Ruby
    48 weeks 4 days ago
  • Links provided
    1 year 11 weeks ago
  • Module for wordpress to Drupal 6.x
    1 year 11 weeks ago
  • The wordpress plugin looks
    1 year 13 weeks ago
  • Good point..... You're
    1 year 13 weeks ago
  • Many thanks. If I'm going to
    1 year 13 weeks ago

New forum topics

  • Imported posts only visible to user1
  • What should the port number be
  • WordPress MU?
  • funny little bug in mac version
  • Error: Unable to Insert into Node_revisions table when converting from wordpress 2.6.0 to drupal 6.4
more

Sponsored links

Steve's Stuff
All about my running and from time to time other stuff

Improve Memory
All about memory techniques, excellent and relevant articles.

Enhancement # 2 - Valid Input Range

Submitted by Steve on Tue, 29 Apr, 2008 - 19:49
  • C++
  • cin

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.

   while (!(cin >> rows) or (rows < 1 or rows > 79))
    {
        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:

while (!(cin >> rows) || (rows < 1 || rows > 79))

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.

‹ Enhancement # 1 - Trap non numeric input up Enhancement # 3 - Trap number followed by non numeric ›
  • Printer-friendly version
  • Add new comment
  • 478 reads

 Subscribe in a reader

free hit counter


RoopleTheme