-1

I am trying to display total price on simple C# console program. But Control cannot fall through from one case label ('case "1":') to another errors occur. What is my mistake?

    using System;
    class a
    {
        static void Main()
        {

        start: Console.WriteLine("1. Cofee 2. Jam. 3. Bread 4. Apple");
            String a = Console.ReadLine();

            int price = 0;
            switch (a)
            {
                case "1": price += 2;
                case "2": price += 3;
                case "3": price += 4;
                case "4": price += 5;
                    Console.WriteLine("your selected item is {0} Your price is ${1} Do You Want to Continue? YES or NO", a, price);
                    String max = Console.ReadLine();
                    max = max.ToUpper();

                    if (max == "YES")
                    {

                        goto start;

                    }
                    else
                    {

                        break;
                    }


                default: goto start;

            }

        }



    }
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • 3
    You really need to re-think how you have structured this program. Using "goto" is not idiomatic C# and this is NOT a habit you want to form or your code will become a maintenance nightmare. Think about using a loop structure like 'while' to achieve this behaviour instead. – Chris McAtackney Apr 09 '14 at 12:23

6 Answers6

4

I understand your program is a loop and you select an item in each iteration. The price of this item should be added to the total. But the problem is all the prices are added each time.

You need to use break; to stop execution of switch:

switch (a)
{
    case "1": price += 2; break;
    case "2": price += 3; break;
    case "3": price += 4; break;
    case "4": price += 5; break;
}

Remove the rest of the code from switch then.

You can find more on MSDN:

Execution of the statement list in the selected switch section begins with the first statement and proceeds through the statement list, typically until a jump statement, such as a break, goto case, return, or throw, is reached. At that point, control is transferred outside the switch statement or to another case label.

Szymon
  • 42,577
  • 16
  • 96
  • 114
  • The OP wants to fall-through i.e. execute the matching case statement and those below – W.K.S Apr 09 '14 at 12:21
  • @W.K.S I'm pretty sure he doesn't. That looks very unlikely. – Kendall Frey Apr 09 '14 at 12:22
  • Really? Isn't that what he meant by `Control cannot fall through from one case label ('case "1":') to another` – W.K.S Apr 09 '14 at 12:23
  • @W.K.S I think he doesn't want fall-through. Looking at the code, it's a loop (with goto!) and you select an item in each iteration. The price of this item should be added to the total. But the problem is all the prices are added each time. – Szymon Apr 09 '14 at 12:24
  • I agree. Looks like he ASSUMES it will not fall through. THe code otherwise makes zero sense. Clarly that is an issue of his understanding of .NET. – TomTom Apr 09 '14 at 12:51
4

You need to insert a break; statement between each case.

Also, it looks like you want that code to display your message if the selected choice is 1 - 4. You can rework your logic a bit to accomplish that, removing the need for a goto:

while (true)
{
    Console.WriteLine("1. Cofee 2. Jam. 3. Bread 4. Apple");
    String a = Console.ReadLine();

    int price = 0;
    switch (a)
    {
        case "1": price += 2; break;
        case "2": price += 3; break;
        case "3": price += 4; break;
        case "4": price += 5; break;
        default: continue;
    }

    Console.WriteLine("your selected item is {0} Your price is ${1} Do You Want to Continue? YES or NO", a, price);
    String max = Console.ReadLine();
    max = max.ToUpper();

    if (max == "NO")
        break;
}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
3
            case "1": price += 2;
                      goto case "2";
            case "2": price += 3;
                      goto case "3";

etc

theUser
  • 1,346
  • 3
  • 21
  • 40
1

Not being able to fall through cases is a restriction in the C# language specification. What you're doing wrong is trying to fall through.

Tony Vitabile
  • 8,298
  • 15
  • 67
  • 123
  • More exact: A restriction in the langauge design - and documented in the language. The compiler just follows the language specifications. – TomTom Apr 09 '14 at 12:21
  • I've updated my answer to reflect that it's a restriction in the C# language spec. – Tony Vitabile Apr 09 '14 at 12:22
0

In order to get fallthrough like behavior you need to explicity state that you mean to fall through. This is done using goto. ex:

switch (a)
{
    case "1": price += 2; goto case "2";
    case "2": price += 3; break;
}

http://msdn.microsoft.com/en-us/library/06tc147t.aspx

Mitch
  • 191
  • 8
0

Your switch block should probably look like this (note the break; statements):

switch (a)
{
    case "1":
        price += 2;
        break;
    case "2":
        price += 3;
        break;
    case "3":
        price += 4;
        break;
    case "4":
        price += 5;
        break;
    default: goto start;
}

It also looks like you want this section of code to run after the switch, not just if they selected Apple.

Console.WriteLine("your selected item is {0} Your price is ${1} Do You Want to Continue? YES or NO", a, price);
String max = Console.ReadLine();
max = max.ToUpper();

if (max == "YES")
{
    goto start;
}
else
{
    break;
}

You will probably need to reorganize your entire program. I think this is what you want to do:

using System;
class a
{
    static void Main()
    {

    start: Console.WriteLine("1. Cofee 2. Jam. 3. Bread 4. Apple");
        String a = Console.ReadLine();

        int price = 0;
        switch (a)
        {
            case "1":
                price += 2;
                break;
            case "2":
                price += 3;
                break;
            case "3":
                price += 4;
                break;
            case "4":
                price += 5;
                break;
            default: goto start;
        }
        Console.WriteLine("your selected item is {0} Your price is ${1} Do You Want to Continue? YES or NO", a, price);
        String max = Console.ReadLine();
        max = max.ToUpper();

        if (max == "YES")
        {
            goto start;
        }
    }
}
Kendall Frey
  • 43,130
  • 20
  • 110
  • 148