Hmm, interesting. If you know how to grab the input from the text box then I can help you. I haven't delved that far into WinAPI/MFC/X to help you with that, but I can help you if you have the input in string format. (or a format similar stringstream, const char [], char *, etc.)
Let's think about an integer. An integer only contains whole numbers, but integers also contain negative numbers. Therefore, integers can only contain the characters "0,1,2,3,4,5,6,7,8,9,-" I would also include space or tab since people have a tendency to press those keys after inputs. I would parse through the string and make sure that each of the characters is equal to one of those values.
I'm away from my computer that has my compiler on it. (I'm using ChromeOS, it stinks.) So my code is probably riddled with bugs, but this should be what you need to do in general. I'm going to fix it when I get home.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
bool checkInteger(std::string input)
{
/* convert the string to const char */
const char c_input [] = input.c_str();
/* iterate through the input */
/* you may want to change this to the size of c_input */
for (int i = 0; i < input.size(); i++)
{
/* check if the character is not an integer nor a negative sign */
if ((c_input[i] < 48 ) && (c_input[i] > 57 ) && (c_input[i] != 45))
{
return false;
}
}
/* no violations have been detected, this is an integer */
return true;
}
| |
There is probably much faster ways of doing this, but I hope that this will help you along.
Edit:
If you want to use the value once you found out it is an integer, you could do something like:
1 2 3 4
|
if (checkInteger(input))
{
int number = atoi(input.c_str());
}
| |
Good Luck,
MaxterTheTurtle