Hello everyone

Thanks for viewing this topic for me.... my question is when I tried to check user inputs a valid age it works only if a number is entered. If by mistake user inputs a character value (e.g. any character a to z or any symbol) this loop runs infinite

How can I check if a user inputs a number other then numerical number display error and take him/her back to same question again?

below is my code of age check

        do
        {
              printf("\tEmployee Age (%2d to %2d): \t", min_age, max_age);
              scanf("%d", &age);
        }while(age < min_age || age >max_age);//employment age between 18 to 65

Same issue with below code too, but this is the opposite of numeric ....I want user only puts a character value no numeric value should be accepted.... plus one other issue is if user puts a wrong character twice (like 'kk' instead of 'k' ) still error

        do
        {
              getchar();
              printf("\tEmployee Gender   (M/F): \t");
              scanf("%c", &gender);
        }while(gender != 'M' && gender != 'm' && gender != 'F' && gender != 'f');

I would really appreciate any comment or help
Thanks alot
Sharif

>>How can I check if a user inputs a number other then numerical number display error and take him/her back to same question again?

Get input as a character array insted of int then you can check for non-digit entries. If its ok then convert it to an int.

thank you very much Ancient Dragon thanks for your time and effort :) thanks once again
sharif

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.