2022 - You cause an exception if you enter a card number out of the range 1-5

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

Issue

When you are asked to enter a card number you can enter invalid values (ie not 1 to 5). The GetCardChoice method of the BreakThrough class is what gets the value from the user, this is the code below:

        private int GetCardChoice()
        {
            string Choice;
            int Value;
            do
            {
                Console.Write("Enter a number between 1 and 5 to specify card to use:> ");
                Choice = Console.ReadLine();
            }
            while (!int.TryParse(Choice, out Value));
            return Value;
        }

This method currently will ignore any non integer values entered.

What you need to do

  • You could start by moving the 'int.TryParse(Choice, out Value)' into the end of the do section.
  • You could then change the while condition to test if value is < 1
  • You could then also test if values is > 5
  • The 2 conditions above will need to be joined together with an OR ( || in C#, shift & the key above the windows key)
  • This method should still not accept non integer values