-
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)?
-
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:
-
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
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
|
Bookmarks