Can someone explain how to validate format of user input for SSN and Phone Number?
SSN format should be ###-##-####
and phone number should be ###-###-####
So far I can check the number of digits, and that they are all digits, but i don't know how to check for the above format...I can't find any examples on my text, and I dont think I understand string functions as well as I could, so any help would be appreciated!
Well, I coded an example for you for about 5 minutes and cplusplus.com decided I was no longer logged in and I have to redo it all which I'm not going to do, sorry.
Basically the gist of it all is use a parser/tokenizer such as bison/flex or boost.spirit, or spend a lot more time writing it out by hand for almost no benefit at this point.
You can do this one by hand since it won't take long though. Basically, just check for the size of the string to make sure it's the correct size, then iterate through each character in the string to make sure it's the character its supposed to be. Its not hard at all, try not to over-complicate things. I would look into the concept of a grammar "token". You can use std::string.find() to see if a character is inside of a token rule.
I'm not sure how atoi is supposed to handle dashes... probably not very conforming either.
I suppose you could just remove the dashes in the string but this assumes they're in the correct location and that the rest of the string has valid numeric characters. I'm not sure the best way to go about this. I would think parsing the string as is would be the safest bet but probably not the fastest one.
Ok, so I have gotten it EXCEPT for...isdigit. It is not checking every instance, i can still enter abc-12-1234 and it allows me to. I need them to ALL be numbers
Before I add that, can you look this over and let me know if you see any errors in this? I redid a few things and found that this one works the best so far...i have tried almost every entry possible and i am getting the correct outcome...