Reputation: 1543
This partial code check for specific format in char array.
char emp_id[10];
cout<<"Employee ID\t\t: ";
while(cin.getline(emp_id,10)) {
if (emp_id[0] == 'e' || emp_id[0] == 'E'){
break;
}
std::cout << "Input error. Invalid employee ID format." << std::endl;
cout<<"\nEmployee ID\t\t: ";
}
The accepted format is e<employee ID number>
. For example: e3
or E59
. Any letter after e
are not accepted such as Eg
, e56h
, e77$
etc. etc.
I manage to check whether the first letter in array is e
or E
with the above code. Then I didn't know how to check for invalid format such as Eg
or e56h
. If this question had been asked before please point me to the answer page because I'm not sure what search keyword should I use. Please help me and thanks in advance.
Upvotes: 0
Views: 192
Reputation: 34367
Try using substring after first char and converting into int as below:
int myNum = atoi(emp_id.substr(1, emp_id.length()).c_str());
If successful then good otherwise fail it.
Upvotes: 1