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

  • Got it solved This page here
    53 min 55 sec ago
  • Links working
    1 day 41 min ago
  • Thanks... I may be able to
    1 day 6 hours ago
  • 3306 by default
    1 day 16 hours ago
  • Is this the right place to
    1 day 17 hours ago
  • Figured
    3 weeks 6 days ago
  • I'm guessing at this stage
    3 weeks 6 days ago
  • WordPress MU?
    4 weeks 1 hour ago
  • Thanks
    10 weeks 1 day ago
  • I'm running the conversion
    11 weeks 4 days ago

New forum topics

  • 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
  • index.php?
more

Who's online

There are currently 0 users and 2 guests online.

Who's new

  • oODeathStormOo
  • leruffiant
  • Emtee
  • mnogodet
  • ZioMimmo

Enhancement # 2 - Valid Input Range

Submitted by Steve on Tue, 29 Apr, 2008 - 20: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
  • Login or register to post comments
  • 199 reads

 Subscribe in a reader

free hit counter


RoopleTheme