Home > Mobile >  c# Switch case does not continue after try catch - Need a loop to enter a valid data type
c# Switch case does not continue after try catch - Need a loop to enter a valid data type

Time:11-27

When i introduce the "try" "catch" statements and read the user input for the "choice" variable, the code executes as it should it does not however continue on to the "switch" "case" statement as the "choice" variable is then said to be an unassigned local variable. Please help.

I was expecting the rest of the "switch case" code to execute.

int choice;

do
{
    Console.Clear();
    Console.WriteLine("1. Load Data");
    Console.WriteLine("2. Add Customer");
    Console.WriteLine("3. Sort Customers");
    Console.WriteLine("4. Display Customers");
    Console.WriteLine("5. Edit Customer");
    Console.WriteLine("6. Exit\n");
    Console.WriteLine("Chose an option 1-6: ");

    bool valid = true;
    do
    {
        try
        {
            choice = Convert.ToInt32(Console.ReadLine());
        }
        catch (Exception)
        {
            valid = false;
            Console.WriteLine("please enter a number from 1 - 6");
        }
    } while (valid == false);
    switch (choice)
    {
        case 1:
            break;
        case 2:
            break;
        case 3:
            break;
        case 4:
            break;
        case 5:
            break;
        default:
            break;
    }
    Console.Clear();
} while (choice != 6);

CodePudding user response:

When the catch block is executed, then choice is not assigned a value in the try block.

Assign a value to choice in the catch block

int choice; // Choice is declared here but is not assigned a value.

...

try
{
    // When the conversion fails, no assignment to `choice` is made.
    choice = Convert.ToInt32(Console.ReadLine());
    valid = true; // This was also missing.
}
catch (Exception)
{
    valid = false;
    choice = 0; // <========================================
    Console.WriteLine("please enter a number from 1 - 6");
}

...

// Now, here `choice` is known to have a value assigned in any case.

Note that the compiler does not consider the value of valid nor does the compiler analyze how it affects the control flow when is says that choice may be unassigned. The error message is based solely on a static analysis of the control flow for assignments made to choice itself.

But it would be easier to use TryParse instead of catching an exception.

int choice;
do {
    // Console output ...
    while (!Int32.TryParse(Console.ReadLine(), out choice)) {
        Console.Write("please enter a number from 1 - 6: ");
    }
    Console.WriteLine();
    switch (choice) {
        ...
        default:
            break;
    }
    Console.Clear();
} while (choice != 6);

CodePudding user response:

Sir I think i have just solved it. I have added a Break; statement to the try block. in this case the code loops back to the beginning and allows the user to enter the correct data type again and the switch statement continues.

            bool valid = true;
            do
            {
                try
                {
                    choice = Convert.ToInt32(Console.ReadLine());
                    break;
                    
                }
                catch (Exception)
                {
                    Console.Clear();
                    valid = false;
                    choice = 0;
                    Console.WriteLine("please enter a number from 1 - 6");
    

                }
                
            } while (valid == false);

CodePudding user response:

You can just assign a value when you decleare choice

int choice = 0;

You have also to set the valid variable at true at the start of the loop.

            bool valid;
            do
            {
                valid = true;
                try
                {
                    choice = Convert.ToInt32(Console.ReadLine());
                }
                catch (Exception)
                {
                    valid = false;
                    Console.WriteLine("please enter a number from 1 - 6");
                }
            } while (valid == false);
  • Related