stop User from using CHAR or STRING when INT is requested

I've noticed that my program crashes when the user enters anything other than an integer when I ask for it.
(I know how to make my program reject integers I don't want my user to use,
(I use system("CLS";))

Is it just better to use strings and the switch function to convert any user input into numbers.(I would know how to reject the strings I don't want)
What is the "switch function to convert any user input into numbers"?
See http://www.cplusplus.com/forum/articles/6046/
thanks bazzy
There is also this information for validating integers. (As opposed to input like "4birds-in-the-hand".)
http://www.cplusplus.com/forum/beginner/13044/page1.html#msg62827
Good luck!
Take a look at this for an example on a simpler approach. The solution for you will depend on how much validation you need to do. The extra validation and copying in the previous solutions might be necessary or it might not be depending on what you want to do. I do like the idea presented by earlier posters about designing a generic solution that you only have to write once. However that solution could be simpler if you don't need to worry about the extra validation. If someone enters the correct integer and then fat fingers a key accidentally does your program want to say that it is an error or just ignore the extra text at the end?
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.3

I'd also read some of the other IO sections if I were you. Between all of these links I am sure that you'll be able to pick the best solution for your program. You want to embed the read functionality within a function to hide the implementation from the caller so that you don't have to keep rewriting the same code to validate the input over and over. Extra points for you if you can make it a template function that handles any type!
http://www.parashift.com/c++-faq-lite/input-output.html

Topic archived. No new replies allowed.