how to close a console application from inside loop with 2 switches
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":
}
c# while-loop switch-statement exit
|
show 7 more comments
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":
}
c# while-loop switch-statement exit
you couldreturn
from the Main method.
– Mong Zhu
Nov 12 '18 at 15:02
Possible duplicate of 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
1
i'll be sure to do that in future
– Jack McCann
Nov 12 '18 at 15:18
|
show 7 more comments
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":
}
c# while-loop switch-statement exit
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":
}
c# while-loop switch-statement exit
c# while-loop switch-statement exit
edited Nov 12 '18 at 15:12
Jack McCann
asked Nov 12 '18 at 15:00
Jack McCannJack McCann
249
249
you couldreturn
from the Main method.
– Mong Zhu
Nov 12 '18 at 15:02
Possible duplicate of 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
1
i'll be sure to do that in future
– Jack McCann
Nov 12 '18 at 15:18
|
show 7 more comments
you couldreturn
from the Main method.
– Mong Zhu
Nov 12 '18 at 15:02
Possible duplicate of 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
1
i'll be sure to do that in future
– Jack McCann
Nov 12 '18 at 15:18
you could
return
from the Main method.– Mong Zhu
Nov 12 '18 at 15:02
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
– Azzy Elvul
Nov 12 '18 at 15:02
Possible duplicate of 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
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
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
1
1
i'll be sure to do that in future
– Jack McCann
Nov 12 '18 at 15:18
i'll be sure to do that in future
– Jack McCann
Nov 12 '18 at 15:18
|
show 7 more comments
1 Answer
1
active
oldest
votes
i just needed to place a break statement after environment.exit(-1) instead of beforehand
case "quit":
Environment.Exit(-1);
break;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264818%2fhow-to-close-a-console-application-from-inside-loop-with-2-switches%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
i just needed to place a break statement after environment.exit(-1) instead of beforehand
case "quit":
Environment.Exit(-1);
break;
add a comment |
i just needed to place a break statement after environment.exit(-1) instead of beforehand
case "quit":
Environment.Exit(-1);
break;
add a comment |
i just needed to place a break statement after environment.exit(-1) instead of beforehand
case "quit":
Environment.Exit(-1);
break;
i just needed to place a break statement after environment.exit(-1) instead of beforehand
case "quit":
Environment.Exit(-1);
break;
edited Nov 12 '18 at 15:47
Tilman B. aka Nerdyyy
689
689
answered Nov 12 '18 at 15:16
Jack McCannJack McCann
249
249
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264818%2fhow-to-close-a-console-application-from-inside-loop-with-2-switches%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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
– 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
1
i'll be sure to do that in future
– Jack McCann
Nov 12 '18 at 15:18