I am working on a project where the user is prompted to enter a zip code. I need to verify it is a five digit number (I don't need to verify it is an actual zip code).
Here is a portion of my code.
string userInput;
cout << "Zip Code> ";
getline(cin, userInput, '\n');
while (stoi(userInput)<10000 || stoi(userInput) > 99999){
cout << endl << endl << "You must enter a valid zip code. Please try again." << endl;
cout << "Zip Code>" << endl;
getline(cin, userInput, '\n');
}
PropertyRec.setZipCode(stoi(userInput));
This works fine unless the zip code starts with a zero. If it does, the validation is no good and the initial zero never saves to the variable once the input string is converted to an integer.
Should I leave the zip code as a string when saving to the database? If so, how can I verify there are exactly 5 characters and each character is numeric?