2

My program runs from the command line and requires the user to enter booleans (yes or no) and integers (numbers without decimals). For this I have two methods to check to make sure the user makes a valid entry, for example if the user is asked what temperature it is outside "cheese" isn't a valid response. What should happen if the user gives invalid input? Should the same prompt be repeated? Should it remind the user of the input type needed?

For example:

Please enter your height in cm: xyz
Please enter an integer.
Please enter your height in cm: 180
Do you wear glasses: 5
Please enter y/n.
Do you wear glasses: blah
Please enter y/n.
Do you wear glasses: y

Also, is it clear if a boolean is expected to tell the user to answer using y/n?

7

Since it's a CLI, I don't think you need to explain too much. I'm assuming the end user is somewhat tech savvy, so they should be used to providing input the way you describe. My only recommendation is adding a hint at the end. i.e

Do you wear glasses (y/n):
0

I agree with Paul. If you have chosen CLI, then I guess your users understand what should be entered.

But manual or typo errors will occur. Humans make mistakes. Even for boolean inputs "yes" may not be accepted in place of "y". This scenario can extend to integers as well, where user might type a comma instead of a dot.

The UI should let the user handle these errors effectively. The best way is to inform the user of these three points

  1. Let the user know what he has entered
  2. Why is it invalid
  3. What is a valid input (may be add an example as well)

Please enter your height in cm (Integers only. Ex: 164): xyz

Sorry, xyz is an invalid input. Please enter only an integer. Please enter your height in cm (Integers only. Ex: 164): 180

Do you wear glasses(y/n): 5

Sorry, 5 is an invalid input. Please enter only y/n. Do you wear glasses(y/n): y

0

I would suggest to include (y/n) in the question itself, like this:

Do you wear glasses? (y/n):

If user enters a wrong answer, just display back the same question again. I believe this will be self-explanatory:

Do you wear glasses? (y/n):5
Do you wear glasses? (y/n):

..

What's your height? (cm):172

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.