Paigey Swenson
Paigey Swenson

Reputation: 9

errors using default for switch statement c#

im trying to use default at the end of my switch statement and i keep getting errors! im getting frustrated please help

im very new to programming and its quite confusing lol

case "d":

    Console.WriteLine("Please enter your option here:");
    Console.WriteLine("First number");
    double firstNum = Int32.Parse(Console.ReadLine());
    Console.WriteLine("Second number:");
    double secondNum = Int32.Parse(Console.ReadLine());
    double sum = firstNum / secondNum;
    Console.WriteLine("Result:" + sum);
    Console.WriteLine("\n");
    Console.ReadKey();
    break;
}
default:

    else if (number == 2)

    {

        Console.WriteLine("Exiting the code");
        Console.WriteLine("Please press any key to exit");
        Console.ReadLine();
        break;
    }

    else
    {

        Console.WriteLine("Wrong selection");
        Console.WriteLine("Please press any key to go to the main menu");
        Console.ReadLine();
Active
Error   CS1002  ; expected  ConsoleApp50        69  Active
Error   CS1513  } expected  ConsoleApp50        69  Active
Error   CS1513  } expected  ConsoleApp50        98  Active

Upvotes: 0

Views: 508

Answers (1)

Gabriel Luci
Gabriel Luci

Reputation: 40998

When the code is that weird, sometimes the error messages don't make sense :)

A few problems I see:

  1. At the end of your case "d": block, you have a } where there was no previous {. Unless there is code in your case "d": block that you're not showing us, then you can just delete that }.
  2. In your default: block, you're starting with an else if, where there was no if before that. You can only use else if after an if. Unless there's code you're not showing us, then you can change the else if to just if.
  3. There's no } at the end, to close the else block.
  4. You still need a break; at the end of the default: block.

Upvotes: 3

Related Questions