Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
This is the kind of long scenario question that ends Paper 2. Answer in pseudocode.
A website asks a new user for a username, an age and a password. Write an algorithm that:
Then dry-run it for the inputs sam, samirak, , , sun, son, moon42, moon42. How many error messages are output, and how many inputs are taken?
Validation checks that data is sensible: range, length, type, presence and format checks, and a check digit. Verification checks that data was entered as intended: a visual check and a double entry check. A validation loop repeats the input, with an error message, until the data is acceptable.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
A model answer.
// Sign-up checks
REPEAT
OUTPUT "Enter a username (6 to 12 characters)"
INPUT Username
IF LENGTH(Username) < 6 OR LENGTH(Username) > 12
THEN
OUTPUT "Username must have 6 to 12 characters"
ENDIF
UNTIL LENGTH(Username) >= 6 AND LENGTH(Username) <= 12
REPEAT
OUTPUT "Enter your age"
INPUT Age
IF Age < 13 OR Age > 99
THEN
OUTPUT "Age must be 13 to 99"
ENDIF
UNTIL Age >= 13 AND Age <= 99
REPEAT
OUTPUT "Enter a password"
INPUT Password
OUTPUT "Enter the password again"
INPUT Again
IF Password <> Again
THEN
OUTPUT "The two passwords do not match"
ENDIF
UNTIL Password = Again
OUTPUT "Welcome, ", UsernameThe run.
sam has characters: message . samirak has : accepted.sun and son differ: message . moon42 and moon42 match: accepted.Welcome, samirak.That is error messages and inputs.
A marking list. One mark for each point.
LENGTH used for the username.<> or =.Answer: error messages and inputs.
What if? The age box could also have a type check, so that text such as fifteen is refused before the range check.
Log in to join the discussion.
Loading…