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


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)
  • Changing Hosts - a Dummies Guide
  • Wordpress to Drupal

Recent comments

  • Got it solved This page here
    3 hours 4 min ago
  • Links working
    1 day 2 hours ago
  • Thanks... I may be able to
    1 day 8 hours ago
  • 3306 by default
    1 day 18 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 3 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

peek()

Enhancement # 3 - Trap number followed by non numeric

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>

  • Login or register to post comments
  • Read more
  • 206 reads

 Subscribe in a reader

free hit counter


RoopleTheme