Checking inputs — Computing, 7–10
Programs often receive information from people, but people can type a number, word or date incorrectly. Input checking spots values that cannot make sense for the task and asks for a correction before the program uses them.
A sensible gate for data
Input checking is a gate between what someone types and what a program does with it. The gate asks simple questions: Is this a number? Is it in the allowed range? Is anything missing? If the answer is no, the program explains what to fix instead of quietly using nonsense.
Why reject an impossible answer?
A program cannot guess what you meant when you type 250 as your age or leave a required answer blank. If it accepts the value, later calculations may look neat but be wrong. Checking inputs moves the problem to the start, where the person can see it and correct it.
Checking a temperature
A form asks for today's temperature in degrees Celsius, from −50 to 50. First, someone types 18: it is a number and lies in the range, so it is accepted. Next, someone types 81: the program rejects it because it is outside the range. Finally, someone types warm: it asks for a number instead.
A check is not a guess
It is tempting to make a program accept anything and let a later step sort it out. But the program may not know whether 12 means twelve apples, twelve years or 12 o'clock. A check should reject values that break the task's rules, not reject a value merely because it is unusual or different from your own answer.
Forms, games and machines
Online forms check email shapes, dates and required boxes before sending your answers. Games check that a move is allowed, and a ticket machine checks that a payment or travel choice makes sense. These checks reduce confusion, but they cannot prove that the information itself is truthful.
Keep exploring
Other languages
Loading MyLeoNes™…