0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.