-3

Hi what have I done wrong, I'm getting the error "Control cannot fall through from one case label to another" at line 15 (Switch (z))

   using System; 
   namespace test
   {
    class MainClass
     {
     public static void Main (string[] args)
     {
        Console.WriteLine ("Velkommen til pCalc! Vælg hvad du skal I menuen:");
        Console.WriteLine ("1. Phythagoras");
        Console.WriteLine ("2. Cirklens areal og omkreds");
        Console.WriteLine ("3. + - * eller /");

        int z = Convert.ToInt32(Console.ReadLine());
        switch (z)
        {
        case 1:
            Console.WriteLine ("Her skal du angive 2 værdier, a og b for at beregne c");
            Console.WriteLine ("Skriv a værdien: ");
            double a = double.Parse (Console.ReadLine ());
            Console.Clear ();
            Console.WriteLine ("Skriv værdien for b: ");
            double b = double.Parse (Console.ReadLine ());
            Console.Clear ();
            Console.WriteLine (Math.Sqrt((Math.Pow(a, 2))+(Math.Pow(b, 2))));
            break;

        case 2:
        Console.WriteLine ("Skriv radius a cirklen: ");
        double r = double.Parse (Console.ReadLine ());
        double areal = (Math.Pow (r, 2) * Math.PI);
        Console.Clear ();
        Console.WriteLine (areal);
        Console.WriteLine ("Vil du også vide omkredsen? Skriv 1 for Ja, 2 for Nej");
        int q = Convert.ToInt32 (Console.ReadLine ());
        switch (q) {
        case 1:
            Console.WriteLine (r * 2 * Math.PI);
            break;
        case 2:     
            break;
        }
        }   
    }
}

}

Rajasekar Gunasekaran
  • 1,799
  • 3
  • 24
  • 40
user2923446
  • 399
  • 1
  • 4
  • 5
  • 9
    You forgot `break;` at the end of the first `case 2`. Also, [trivial syntax errors do not make good questions](http://meta.stackexchange.com/questions/137395/are-questions-solved-by-fixing-a-typo-or-basic-syntax-error-too-localized). – Zong Oct 26 '13 at 17:19
  • Ok I identified the location of the problem, and it's at my Case 2: – user2923446 Oct 26 '13 at 17:20
  • possible duplicate of [Control cannot fall through from one case label](http://stackoverflow.com/questions/6696692/control-cannot-fall-through-from-one-case-label) – Shiplu Mokaddim Oct 26 '13 at 17:20
  • Thank you Zong Zheng I didn't even notice that, I feel so dumb ^-^ – user2923446 Oct 26 '13 at 17:26

2 Answers2

4

You have to terminate every case (even the last one!) with a break statement. Specifically in your case:

case 2:
        Console.WriteLine ("Skriv radius a cirklen: ");
        double r = double.Parse (Console.ReadLine ());
        double areal = (Math.Pow (r, 2) * Math.PI);
        Console.Clear ();
        Console.WriteLine (areal);
        Console.WriteLine ("Vil du også vide omkredsen? Skriv 1 for Ja, 2 for Nej");
        int q = Convert.ToInt32 (Console.ReadLine ());
        switch (q) {
        case 1:
            Console.WriteLine (r * 2 * Math.PI);
            break;
        case 2:     
            break;
        }
break; // ← mandatory!
knittl
  • 246,190
  • 53
  • 318
  • 364
-1

One clarification here.

Answers here show you must have a break, and that is mostly true except in one coding case. If the "break" is unreachable, then it's not required and leaving it out quiets the compiler warning.

Consider if a case "returns" either directly or through a series of conditionals.

<code>
case 1:
    if (Red) { return 6;} else {return 10;}
case 2:
    if (Blue) {return 4;} else {return 50;}

Both conditional conditions must return (or break, I suppose) but as long as all code streams get you out of the switch, the compiler is fine with it.

user3158591
  • 119
  • 3