0

i'm creating a simple calculator and i've been trying to use switches to allow the user to select whether they want to add, subtract, multiply or divide their inputted values.

They can also type quit to exit the console. but after case quit: when i add a method of closing the console such as Environment.Exit(-1);

It throws up an error and using break; just brings me into my next switch (both of which are in the same while loop as the second one restarts the console).

Any ideas on what i can do?

            {
                case "add":
                    answer = (num1 + num2);
                    Console.WriteLine("your answer is {0:0.00}", answer);
                    break;
                case "subtract":
                    answer = (num1 - num2);
                    Console.WriteLine("your answer is {0:0.00}", answer);
                    break;
                case "multiply":
                    answer = (num1 * num2);
                    Console.WriteLine("your answer is {0:0.00}", answer);
                    break;
                case "divide":
                    answer = (num1 / num2);
                    Console.WriteLine("your answer is {0:0.00}", answer);
                    break;
                case "quit":

            }       
Jack McCann
  • 29
  • 10
  • you could `return` from the Main method. – Mong Zhu Nov 12 '18 at 15:02
  • Possible duplicate of [Correct Way to Exit From Console Application](https://stackoverflow.com/questions/35393877/correct-way-to-exit-from-console-application) – Azzy Elvul Nov 12 '18 at 15:02
  • What error do you get when you use _"Environment.Exit(-1); "_? – PaulF Nov 12 '18 at 15:05
  • PaulF it tells me that control cannot fall out of switch from final case label (case "quit":) – Jack McCann Nov 12 '18 at 15:07
  • It sounds like you are missing the break statement - required even though you are exiting the application. Also note - to tag someone in a comment, precede the name with @ - e.g. @PaulF - then they will be notified of your reply. – PaulF Nov 12 '18 at 15:09
  • putting a break statement before the environment.exit causes it to skip over the environment.exit and into my next switch inside my while loop – Jack McCann Nov 12 '18 at 15:10
  • It needs to go after : _"case "quit" : Environment.Exit(-1); break;"_ – PaulF Nov 12 '18 at 15:11
  • the break should be after the exit, obviously. also, you could just `return -1` from main, assuming this isn't in another method. by the way, from what we see this looks like a successful exit, so use exit code 0 @JackMcCann – Azrael Nov 12 '18 at 15:12
  • ahhh makes sense – Jack McCann Nov 12 '18 at 15:13
  • works perfectly now. i'll add that info as the answer – Jack McCann Nov 12 '18 at 15:14
  • For future reference - when asking a question - rather than saying some piece of code "threw up an error" you should be more explicit. If it threw an exception - then give full details of the exception. If, as in this case, it was a compiler error, then give details of that. You will get the correct answer much quicker. Also, show the exact failing code, not the edited version. – PaulF Nov 12 '18 at 15:17
  • 1
    i'll be sure to do that in future – Jack McCann Nov 12 '18 at 15:18

1 Answers1

2

i just needed to place a break statement after environment.exit(-1) instead of beforehand

case "quit":
    Environment.Exit(-1);
    break;
Azrael
  • 175
  • 1
  • 12
Jack McCann
  • 29
  • 10