Home > database >  c# Switch Case not working and says "Input String was not in a correct format"
c# Switch Case not working and says "Input String was not in a correct format"

Time:12-03

I'm trying to end the process when I run my code using a switch case.

Full code:

int[] Slots = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

while (true)
{

    Console.WriteLine("Please enter the number of the slot (1-10)");

    
    int Slot = Convert.ToInt32(Console.ReadLine());
    if (Slots[Slot - 1] == 0)
    {
        Slots[Slot - 1] = 1;
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine("Reservation Successful!");
    }
    else if (Slots[Slot - 1] == 1)
    {
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("Slot preoccupied.");
    }

    Console.ForegroundColor = ConsoleColor.White;
    Console.WriteLine("(R)eserve another slot, (C)heck available slots or (E)nd Process?");

    char choice = Convert.ToChar(Console.ReadLine());

    switch (choice)
    {
        case 'E':
        case 'e':
            break;
        case 'C':
        case 'c':
            Console.WriteLine("The available slots are:");
            for (int i = 0; i < Slots.Length; i  )
            {
                if (Slots[i] == 0)
                    Console.WriteLine("{0}", i   1);
            }
            continue;
        case 'R':
        case 'r':
            continue;
       
        default:
            Console.WriteLine("Invalid Input");
            break;
    }
}

Everything works fine except for the 'E' case.

Whenever I try to use the 'E' case it doesn't perform the break command. Instead it pinpoints to the line:

int Slot = Convert.ToInt32(Console.ReadLine());

And it says that 'Input String was not in a correct format' and when I try to do what the system recommends, which is to change the "int" to "var". It just tells me to change it back to "int" instead.

I am very confused, I have no idea what that means. I am a student and this was never taught to us yet so I don't know what to do.

I expected it to work since it didn't give any errors when i try to run my code, and everything works fine except for the 'E' case.

CodePudding user response:

You seem to think, that the "break" in your switch case will exit your while loop, however the break will only exit the switch case.

The error you are getting, comes from not exiting your while loop. Then the loop executes again and reads an empty line which cannot be parsed to int. Meaning the input string which you are trying to parse into int is not an actual integer (empty line is not an integer).

what you should do is:

bool isRunning = true;
while(isRunning)
{
    //your logic
    switch(choice)
    {
        case 'E':
        case 'e':
            isRunning = false;
            break;
    }
}

CodePudding user response:

A break inside a switch causes the control to only break out of the switch. Hence, you need another break to break out of the while loop. The easiest way to do this is to use an extra boolean variable that is toggled in the 'E' case. After the switch, test if the variable is set, and if so, break out of the loop.

The suggestion to change int to var and back is basically just a coding style suggestion and has no further meaning.

  •  Tags:  
  • c#
  • Related