I am used to programming, as a hobbyist, in C#. I've been trying to get accustomed to C++, but, I have been having issues accomplishing a simple task.
I want to accept input from the user as a string and convert it to an integer or double while simultaneously checking for bad input. (i.e: "54" is acceptable input, but "5&*4" is not.)
In C#, I've accomplished this using
Convert.ToInt32
as shown in the following function (yes, this function only checks if it can/cannot be converted - it doesn't actually return a converted value):
1 2 3 4 5 6 7 8 9
|
public static bool IsInteger (string InputToCheck)
{
try {
Convert.ToInt32 (InputToCheck);
} catch {
return false;
}
return true;
}
|
Although the above may not be the most elegant solution, it does work. I have been trying to duplicate the functionality in C++, but, every solution I've been able to come up with will still allow bad input to get through. For example, using things like atoi will still let "5#$" through and just truncate the value to "5"
Any suggestions? This seems like it should be a simple task -- but, I can't seem to figure it out.
Thanks!