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 22 min ago
  • Links working
    1 day 3 hours ago
  • Thanks... I may be able to
    1 day 8 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 0 guests online.

Who's new

  • oODeathStormOo
  • leruffiant
  • Emtee
  • mnogodet
  • ZioMimmo

Enhancement # 3 - Trap number followed by non numeric

  • View
  • Revisions
Submitted by Steve on Wed, 30 Apr, 2008 - 16:58
  • C++
  • cin
  • get()
  • peek()

As you know, the Asterisk Triangle Program has one remaining flaw and it's a flaw that should be addressed.

Presently, if a user enters as input 50prime the number '50' is accepted as valid and the remaining 'prime' is ignored. If a user enters as input 50.5 the number '50' is accepted and the remaining '.5' is ignored. Ideally we need to trap this and force a true integer to be entered.

Replace this line of code

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

with this

while (!(cin >> rows) or (rows < 1 or rows > 79 or cin.peek() != '\n'))

You'll note that a third condition has been added to the while statement. That being cin.peek() != '\n'. What this is saying is,

"if the next character in the input buffer is not a newline character".

We know that after the execution of cin >> rows that if a true number was entered then all that is left in the input buffer is a newline ('\n') character. The peek() function merely checks for the next character in the input buffer. It doesn't remove the character from the input buffer. An entry such as '50prime' will cause the expression cin.peek() != '/n' to evaluate to true, as it detects the letter 'p', which in turn the body of the while loop is entered.

Here is the complete program code.

// trianglerows-5.cpp
// 30 Apr, 2008.

#include <iostream>
using namespace std;

int main()
{
    int rows, c;
    cout << "Enter the number of rows (1 - 79): ";
    while (!(cin >> rows) or (rows < 1 or rows > 79 or cin.peek() != '\n'))
    {
        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;
    }
    for (int r = 1; r <= rows; r++)
    {
        for (c = 1; c <= rows - r; c++)
            cout << ".";

        for (; c <= rows; c++)
            cout << "*";
        cout << endl;
    }
    // exit routine
    cout << "\n\n...Press 'ENTER' key to EXIT...\n";
    while (cin.get() != '\n')
        continue;                   // clear out non newline characters
    cin.get();                      // user to hit 'ENTER' key
    return 0;
}

I think I've covered all aspects of validating against incorrect data input.

Please note that there are other ways to accept keyboard input and different methods to validate against incorrect keyboard input. Feel free to make suggestions.

‹ Enhancement # 2 - Valid Input Range up Array Solution - Half and half, 10 per line ›
  • Printer-friendly version
  • Login or register to post comments
  • 206 reads

 Subscribe in a reader

free hit counter


RoopleTheme