ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
If you just want to ignore some characters, you can use the method:
istream& istream::ignore ([int N] [, int DELIM]).
For more info, type:
info iostream streams istream "Istream Housekeeping"
If you want to actually find the invalid char's, you'll have to read a string and scan it yourself using a loop. For reading strings choose a method from this info page:
info iostream streams istream "string input"
If the info-doc's are not on your system you can (depending on your linux distribution) install the "libstdc++2.10-dev" (for Debian-testing), or similar package.
is there a way to have it error and tell the user to try again instead of crashing when they enter an char for an int(cause in menu programs it gets annoying to go through the whole series of options to get back to where you want.
Don't accept input as a number. Accept it as a string. Then you can check the characters in the string one by one against ASCII values. If all of the characters in the string have ASCII values of number 0 to 9, the input is valid and convert the whole string to the numeric and process it. Otherwise, give error message.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.