Validation of string and date input in c++?
I just started c++ today. I am doing some simple registration program. I want to validate the input. I got stuck when validate fullname and birth_date. Here is 开发者_运维技巧my requirements:
- Fullname: I just want to check if its empty and no punctuation
- date_birth: i know this is abit tricky. But if I could validate if the input is valid like: month(1-12), date(1-30) and year (not more than current year) should be enough.
Any quick way to do this?
EDIT:
I tried googled string validation, i am still getting lots of errors. Here is my current code:
string fullname;
do{
cout << endl << "Please enter your fullname";
cin >> fullname;
} while(!ispunct(fullname));
My error message is:
XXXX: no matching function for call to `ispunct(std::string&)'
I already include the library, is this a correct way to check string input. How do you usually do the validation?
EDIT 2:
bool valid;
string fullname;
do{
valid = true;
cout << endl << "Please enter your fullname";
cin >> fullname;
string::iterator it;
for ( it=fullname.begin() ; it < fullname.end(); it++ )
if(ispunct(*it)){
valid = false;
}
} while(!ispunct(fullname));
Its weird, I entered: "!!!", it still by pass. Something is wrong in my code?
Well, I'll try to steer you in the right direction. Firstly, in order to validate the string, you'll need to iterate over it character by character. You can do this using iterators and a for
loop. The string class has a begin()
and end()
method, which you can use to loop over the whole string and examine each character.
Once you're looping over the string, all you need to do is write code to validate it based on your requirements. To make sure there's no punctuation characters, you can use the std::ispunct
function, which will tell you whether or not a character is a punctuation character. If you find any punctuation characters, simply consider it an error.
Your first requirement, checking whether the string is empty, is trivial. The string class has an empty()
method which returns true if the string is empty.
Validating the birthday is more tricky. This is the sort of thing regular expressions were made for. Unfortunately, C++ has no built-in support for regular expressions (at least not until the next version of the standard). If you're interested, Boost has a good regular expression library in the meantime.
Otherwise, you'll need to loop over the string and validate each character. Make sure the string starts with characters that form a word corresponding to a month name, then make sure a parenthesis falls after that, etc. You'll need to decide how to handle white spaces in between characters. This will be tricky, but it's a good practice exercise to become familiar with C++.
Solution for the second requirement can be trivial if you choose different data type to represent date of birth. Constraints you mentioned here are all numeric (1<= day <= 31, 1 <= month <= 12, 1900 <= year <=2010) and date of birth is basically a set of three numbers so consider using struct type for birth_date variable, something like this:
struct Date{
unsigned int day;
unsigned int month;
unsigned int year;
};
Date birth_date = {3, 12, 1983};
When you pass birth_date to function that performs validation, you just need to compare struct members against limits.
精彩评论