Okay so I'm trying to verify a PIN number and Zipcode. The PIN should be 4 digits long and the Zipcode should be 5 digits. How can I do this using Int numbers only? I tried to but some error came up. I settled for string, it works, but obviously it allows letters in addition to numbers. I only want numbers (integers). Any help would be appreciated. Thank you.
I would stick with the string and use one of the string functions to determine if there is some other character other than a digit, something like string.find_first_of() possibly.
#include <cctype>
bool is4Digits (string pin)
{
if (pin.length() != 4)
{
returnfalse;
}
for (size_t i = 0; i < pin.length(); i++)
{
if (!isdigit(pin[i]))
{
returnfalse;
}
}
returntrue;
}
Alternative you could use regular expressions - though it might be a bit of an overkill for such a simple task.
For an explanation of thomas's code, read it and even better than that run through it line by line with some examples. You can find out about isdigit by googling it or looking up the reference section of this site. Same goes for any other stuff that's not clear.