A month number of 13, a mark of −5 or a name typed where an age was expected will make a careless program produce nonsense. A solution must state exactly what it accepts and what it produces, and it must check what it is given.

Specifying Inputs and Outputs

Definition: Input/output specification. A precise statement of the data a solution accepts and the result it must produce, including the meaning, type, allowed range and units of each value.

A complete specification gives four facts about every input: what the value means, what type it is, what range it may take, and in what unit it is measured. The same facts are given for the output. Table 1.10 is a specification for a program that converts a month number to a month name.

Table 1.10: Specification for the month-name problem

Item Meaning Type Allowed values
Input: m Month number Whole number 1 to 12
Output Name of month m Text January to December
Invalid input m outside 1 to 12 — Display an error message

Validating the Input

Definition: Validation. Checking that supplied data obeys the specification before it is used.

Two checks are common. A type check confirms that the value is of the right kind, for example a whole number and not a word. A range check confirms that the value lies within the allowed limits. A value can pass one check and fail the other: 13 is a whole number, but it is not a valid month.

Responding to Invalid Input

The specification must also say what happens when validation fails. The usual choices are to display an error message, to ask for the value again, or to stop. A program should never silently change an invalid value into a valid one, because the user would then receive a result for data that was never entered.

Why Specification Comes First

A clear specification serves three purposes. It tells the programmer exactly what to build. It tells the user what may be entered. And it tells the tester which inputs are valid and what output to expect for each, so that the solution can be judged objectively.

Example 1.10: Validating month numbers

Problem: Using the specification in Table 1.10, decide whether each input is valid: 7, 13, July, 0.

Solution:

  1. 7 is a whole number and lies from 1 to 12. It passes both checks.
  2. 13 is a whole number but lies outside 1 to 12. It fails the range check.
  3. July is not a whole number. It fails the type check.
  4. 0 is a whole number but lies outside 1 to 12. It fails the range check.

Result: Only 7 is valid, and the output for it is "July". The other three inputs produce an error message.

Common mistakes

  • Checking only the type of an input and forgetting its range.
  • Silently replacing an invalid value with a default value.
  • Leaving out the unit, so that a height may be entered in centimetres where metres were expected.

Key takeaways

  • A specification states the meaning, type, range and unit of every input and output.
  • Validation checks input against the specification before it is used.
  • Type checks and range checks are separate and both are needed.
  • The response to invalid input must be decided in advance and must be visible to the user.

Practice questions

  1. What four facts does a specification give about each input?
  2. Which check does the input 150 fail for a mark out of 100?
  3. Why should a program not silently correct invalid input?

Answers to the practice questions

  1. Its meaning, its type, its allowed range and its unit.
  2. The range check, because it is a number but lies outside 0 to 100.
  3. The user would receive a result for data that was never entered, without knowing that anything was changed.

Sources

  • Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit I