Catching datatype related error

0 Wande Shok · March 14, 2015
If you expect a user to enter say an integer value, how do you catch a value of different datatype such as char or string?

Post a Reply


Oldest  Newest  Rating
0 Jesse Lange · March 15, 2015
ive only been doing c++ for a few months. I had an issue where if the user enter anything else other than an int, the out put would be messed up. so I found this code which would take the invlaid input and clear it completely and put it with an if loop inside a while loop.

if (!(cin >> x) || (x >= 31))                          
   cin.ignore(numeric_limits <streamsize>::max(), '\n');

not really sure whats going on in the code im still very much a newbie but maybe it could help. I just learned about catch so im not much help in that area
0 Jesse Lange · March 15, 2015
also had to #include <limits> for that code
0 Jesse Lange · March 16, 2015
ok thx I give that a try
0 Jesse Lange · March 16, 2015
I replaced
cin.ignore(numeric_limits <streamsize>::max(), '\n');


but now im back to having a bug  when the user enters  string or char,  the output goes into an infinite loop. The char or string is being stored into an int
variable. my program asks the user for a number 1 -30 but I don't want a bug if the user enters a char or string. I just learned about
templates maybe I could turn that variable to a template? Any suggestions or thoughts much appreciated .
0 Jesse Lange · March 16, 2015
also I used Microsoft visual 2013, but now using codeblocks. don't know if that makes a difference
0 Jesse Lange · March 17, 2015
I tried but got 502 bad gateway message
  • 1



Used in many types of software including music players, video games, and many large scale applications.

Bucky Roberts Administrator