Thread: erronous user input prevention

  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
    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, 03:25 PM
  2. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  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, 06:37 AM
  5. ~ User Input script help~
    By indy in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 06:01 AM