erronous user input prevention

This is a discussion on erronous user input prevention within the C++ Programming forums, part of the General Programming Boards category; I am trying to foolproof a programme i have written and i was just wondering if there is any call ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    3

    Unhappy erronous user input prevention

    I am trying to foolproof a programme i have written and i was just wondering if there is any call from any library that would allow me to stop or detect if a user has entered a different data type than the one specified.
    Any help would be greatly appreciated!!

  2. #2
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    coverd alot, search the boards.

    in any event what type are u taking in?

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    if you want the user to input a letter say 'y' or 'n' for yes or no, you could do something like this:
    Code:
    while (chrOption != 'y' || chrOption != 'n')
    {
       cout << "\"'y\" for yes or \"n\" for no\n";
       cin >> chrOption;
    
       if (chrOption != 'y' || chrOption != 'n')
          cout << "Please use only \"y\" or \"n\",\n";
    }
    I hope that works.
    Why drink and drive when you can smoke and fly?

  4. #4
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Do you mean stuff like

    double user;
    cin >> user;

    user enters "asdf";

    assert(cin.good()); will abort the program.

    if(cin.fail()) // rdstate(), good(), eof() bad() etc
    {
    cin.clear();
    cin.ignore(80, '\n');
    }

    or similar will try to recover from the error if you're using loops.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  2. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 12:09 PM
  3. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 10:53 AM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 07:37 AM
  5. ~ User Input script help~
    By indy in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 06:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21