Robust and secure programming

EasyMediumHard
12
Question 1
Hard

A developer is writing a stock control system. The system uses a 3 x 3 grid containing nine storage bays.

In the program, a storage bay is referred to by a letter and a number. For example, bay N3 in Figure 1 contains an item marked with X.

Figure 2 shows part of a C# program that checks the bay reference entered by a user.

The bay reference is valid if:

  • there are exactly two characters
  • the first character entered is L, M or N
  • the second character entered is 1, 2 or 3.

Figure 1

LMN
1
2
3X

Figure 2

bool check = false;
while (check == false) {
    string bay = "";
    while (bay.Length != 2) {
        Console.Write("Enter storage bay (e.g. M2): ");
        bay = Console.ReadLine();
        bay = bay.ToUpper();
    }
}

Extend the program from Figure 2 so it completes the other checks needed to make sure a valid bay reference is entered.

Your extended program must:

  • use the variable check
  • repeat the steps until a valid bay reference is entered:
    • get the user to enter a bay reference
    • output an appropriate error message if the bay reference entered is not valid.

You should use meaningful variable name(s) and C# syntax in your answer.

[6]

Robust and secure programming Questions

  1. GCSE
  2. /Computer Science
  3. /Robust and secure programming