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
    3 hours 40 min ago
  • Links working
    1 day 3 hours ago
  • Thanks... I may be able to
    1 day 9 hours ago
  • 3306 by default
    1 day 19 hours ago
  • Is this the right place to
    1 day 19 hours ago
  • Figured
    3 weeks 6 days ago
  • I'm guessing at this stage
    3 weeks 6 days ago
  • WordPress MU?
    4 weeks 4 hours ago
  • Thanks
    10 weeks 1 day ago
  • I'm running the conversion
    11 weeks 5 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 3 guests online.

Who's new

  • oODeathStormOo
  • leruffiant
  • Emtee
  • mnogodet
  • ZioMimmo

Enhancement # 1 - Trap non numeric input

Submitted by Steve on Tue, 29 Apr, 2008 - 20:42
  • C++
  • cin
  • failbit
  • get()

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 >> rows;
cin.get();

and replace with:

while (!(cin >> rows) ) // checking against cin fail state
{
    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:

Enter the number of rows: steve
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.

   while (!(cin >> rows))
    {
        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.

‹ Create a triangle type pattern up Enhancement # 2 - Valid Input Range ›
  • Printer-friendly version
  • Login or register to post comments
  • 286 reads

 Subscribe in a reader

free hit counter


RoopleTheme