verifying user input


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: verifying user input

  1. #1
    Join Date
    Oct 2005
    Posts
    173

    verifying user input

    Code:
    	
    nt x;
    displayMainMenu();
    cin >>x;
    while (!cin)
    {
        cin.clear();
        cin.ignore(100,'\n');
        displayMainMenu();
        cin >>x;
    }

    Ok, the way I have it, if I type in any string such as 12A, the number 12 will be read in to x. If it's 9.A, it will read 9. This only works if the number is in front. If I type A9, it is an error and redisplays the menu.

    how do I make sure input like this is not considered good?

    Thanks!

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    The best practice is to read the input into a string and then validate it: http://www.devx.com/getHelpOn/10MinuteSolution/16975
    It's nearly impssible to teach cin what constitutes valid input, especially when you expect alphanumeric data. Simply read the string and then go through each character to decide if it's good or not.
    Danny Kalev

Similar Threads

  1. Restrict user user to open twice
    By MGunawan in forum .NET
    Replies: 1
    Last Post: 08-03-2006, 12:16 PM
  2. Replies: 6
    Last Post: 07-08-2006, 11:48 PM
  3. Replies: 2
    Last Post: 04-18-2005, 01:01 PM
  4. Check if an application is waiting for user input
    By Gerardo Gomez in forum VB Classic
    Replies: 0
    Last Post: 06-23-2000, 12:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center