DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Brad Guest

    C++, Switch, input validation


    I have a program that has a menu that uses "switch". The input is supposed
    to be an integer (1-6). Everything works fine if the user inputs 1-6.
    BUT,
    it falls into an infinite loop if the user inputs anything else. Is there
    a
    simple solution or uder-defined function that can be added to validate the

    user input (even if mulitple characters or integers)?

  2. #2
    ralph Guest

    Re: C++, Switch, input validation


    "Brad" <moodyblues4me@hotmail.com> wrote:
    >
    >I have a program that has a menu that uses "switch". The input is supposed
    > to be an integer (1-6). Everything works fine if the user inputs 1-6.


    >BUT,
    >it falls into an infinite loop if the user inputs anything else. Is there
    >a
    >simple solution or uder-defined function that can be added to validate the
    >
    >user input (even if mulitple characters or integers)?


    default:




  3. #3
    Danny Kalev Guest

    Re: C++, Switch, input validation



    Brad wrote:
    >
    > I have a program that has a menu that uses "switch". The input is supposed
    > to be an integer (1-6). Everything works fine if the user inputs 1-6.
    > BUT,
    > it falls into an infinite loop if the user inputs anything else. Is there
    > a
    > simple solution or uder-defined function that can be added to validate the
    >
    > user input (even if mulitple characters or integers)?


    To validate input, read the data as a string and then check it to see
    that it's valid. To solve the switch problem, insert a default case:

    switch(x)
    {
    case '1':
    break;
    //...
    case '6':
    break;
    default: // handle all other cases
    break;
    }

    Danny

Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links