How to validate user input in some specific format. For example,
Social Security Number, in the format xxx-xx-xxxx , where x is a digit within the range 0 through 9. Only accept valid Social Security Numbers (with no alphabetic characters)
Employee number, in the format xxx-L , where x is a digit within the range 0 through 9, and the L is a letter within the range A through M.
#include<iostream>
#include <string>
class Employee
{
protected:
string name;
string ssn; // social security number in format xxx-xx-xxxx
string empNo; // employee number in format xxx-L
string date;
public:Employee(){
}
void printEmp()
{
cout << "\n Name : " << name;
cout << "\n Hire Date : " << date;
cout << "\n Soc Sec # : " << ssn;
cout << "\n Employee # : " << empNo;
}
void getName(){
}
void setName(string n){
name = n
}
I am considering you are not taking -
in input. You can simply loop the string and use functions like isalpha
and isdigit
. For specific ranges as A to M You can compare it directly. For example:
string s="ABCXYZ";
bool satisfied = false;
for(int i=0; i<s.size(); ++i){
if(s[i]<="M" && s[i]>="A"){
//this satisfies your criteria
satisfied = true;
}else {
satisfied = false;
break;
}
}
if(!satisfied){
cout<<"Invalid input"<<"\n";
}
For isalpha
and isdigit
functions you can just google them up!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.