Search code examples
c#console-applicationdo-while

C#: Optimizing error prevention with the input console


I've just started learning C# using Rob Miles' C# Programming Yellow Book and some related lab exercises from his website. I did one of them and produced a solution that works...(there's not one provided). I wanted it to not crash if the user inputs nonsensical answers or a string that can't be converted into an integer(I Googled this and found the TryParse method. The textbook briefly taught the try/catch method, but that didn't seem useful for a user interface, i.e. to let the program continue after a bad input). I also set it up so that, in one of these cases, it sends out an alternate message. I've ended up with a somewhat long code... a do/while within a do/while and a do/while within a while. If you have any tips for streamlining this, I'd be much obliged.

Best, Elliot

using System;
using System.IO;

static class Cinema
{
    static void Main()
    {
    int[] selection = new int[] //array for the age requirements of each film
        {
        15, 15, 12, 18, 0
        };
    string filmNumberText; //console input for the film number 
    int filmNumber; //input string parsed as integer 
    string ageText; //console input for age
    int age; //parsed age string
    int ageLimit; //age requirement for selected film

    Console.Write("Welcome to our Multiplex.\n\n");
    Console.WriteLine(@"We are presently showing:
    1. Rush (15) 
    2. How I Live Now (15)
    3. Thor: The Dark World (12A)
    4. Filth (18)
    5. Planes (U)");
    do  //loops as long as input is not between 1 and 5
    {
        do //loops as long as the input is not an integer
        {
            Console.Write("\nEnter the number of the film you wish to see: ");
            filmNumberText = Console.ReadLine();
        }
        while (int.TryParse(filmNumberText, out filmNumber) == false);
    } while (filmNumber < 1 || filmNumber > 5);

    filmNumber = filmNumber - 1; //changes input from 1-5 to 0-4
    ageLimit = selection[filmNumber]; //selects age requirement from array

     //loops as long as input is not an integer

        do
        {
            Console.Write("\nEnter your age: ");
        ageText = Console.ReadLine();
        } while (int.TryParse(ageText, out age) == false); // repeats if input is not an integer



    while (age < 0 || age > 125) //if integer is is too small or too large...
    {
        do
        {
            Console.Write("\nInvalid age. Please enter an age between 0 and 125: ");
            ageText = Console.ReadLine();
        } while (int.TryParse(ageText, out age) == false); //check again if input is an integer
    }
    if (age < ageLimit) //if too young for the given film
        {
            Console.WriteLine("\nAccess denied - you are too young");
        }
    else
        {
            Console.WriteLine("\nPlease call our office at 888-999-2928 to reserve tickets.");
        }

        }
}

Solution

  • I have tried to remove some do while loop from your code. is this what are you looking for?

    public static void Main()
    {
        int[] selection = new int[] //array for the age requirements of each film
        {
        15, 15, 12, 18, 0
        };
        string filmNumberText; //console input for the film number 
        int filmNumber; //input string parsed as integer 
        string ageText; //console input for age
        int age; //parsed age string
        int ageLimit; //age requirement for selected film
    
        Console.Write("Welcome to our Multiplex.\n\n");
        Console.WriteLine(@"We are presently showing:
    1. Rush (15) 
    2. How I Live Now (15)
    3. Thor: The Dark World (12A)
    4. Filth (18)
    5. Planes (U)");
        do  //loops as long as input is not between 1 and 5
        {
    
            Console.Write("\nEnter the number of the film you wish to see: ");
            filmNumberText = Console.ReadLine();
        } while (int.TryParse(filmNumberText, out filmNumber) == false || (filmNumber < 1 || filmNumber > 5));
    
        filmNumber = filmNumber - 1; //changes input from 1-5 to 0-4
        ageLimit = selection[filmNumber]; //selects age requirement from array
    
        //loops as long as input is not an integer
    
        do
        {
            Console.Write("\nPlease enter an age between 0 and 125:");
            ageText = Console.ReadLine();
        } while (int.TryParse(ageText, out age) == false || (age < 0 || age > 125)); //check again if input is an integer
    
        if (age < ageLimit) //if too young for the given film
        {
            Console.WriteLine("\nAccess denied - you are too young");
        }
        else
        {
            Console.WriteLine("\nPlease call our office at 888-999-2928 to reserve tickets.");
        }
    
        Console.Read();
    
    }