Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8380973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:35:43+00:00 2026-06-09T16:35:43+00:00

exit method not working while pressing q return with exception handle error.its a console

  • 0

exit method not working while pressing q return with exception handle error.its a console application should i used both application.exit() and envoirnment.exit() . both doesn’t work . am i doing something wrong . help will be really appreciated.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace calculator_extended
{
    class Program
    {
        static void Main(string[] args)
        {
            int d = 0;

            while (true)
            {
                Console.WriteLine("Press A for addition");
                Console.WriteLine("Press S for subtraction");
                Console.WriteLine("Press M for Multiplication");
                Console.WriteLine("Press D for Divide");
                Console.WriteLine("Press q for Exit");


                char c = Convert.ToChar(Console.ReadLine());

                int a = Convert.ToInt32(Console.ReadLine());
                int b = Convert.ToInt32(Console.ReadLine());

                switch (c)
                {
                    case 'A':
                    case 'a':
                        {
                            d = add(a, b);
                            Console.WriteLine(d);
                            break;


                        }


                    case 'S':
                    case 's':
                        {
                            d = sub(a, b);
                            Console.WriteLine(d);
                            break;
                        }

                    case 'M':
                    case 'm':
                        {
                            d = mul(a, b);
                            Console.WriteLine(d);
                            break;
                        }

                    case 'D':
                    case 'd':
                        {
                            d = div(a, b);
                            Console.WriteLine(d);
                            break;
                        }

                    case 'q':
                        {

                            Environment.Exit(0);
                            break;

                        }
                    default:
                        {

                            Console.WriteLine("Please Enter the correct Character");
                            break;
                        }



                }
            }
        }
            private static int add(int a, int b)
    {

                   return a + b;
    }
               private static int sub(int a, int b)
    {

                   return a - b;
    }
               private static int mul(int a, int b)
    {
                   return a * b;
    }
               private static int div(int a, int b)
    {

                   return a / b;
    }



        }
    }

ok this worked thanks guys for help can you check and let me know if the code is perfect and i am not missing something.thanks

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace calculator_extended
{
    class Program
    {
        static void Main(string[] args)
        {
            int d = 0;

            while (true)
            {
                Console.WriteLine("Press A for addition");
                Console.WriteLine("Press S for subtraction");
                Console.WriteLine("Press M for Multiplication");
                Console.WriteLine("Press D for Divide");
                Console.WriteLine("Press q for Exit");


                char c = Convert.ToChar(Console.ReadLine());
                if (c == 'q')
                {
                    Environment.Exit(0);
                }
                int a = Convert.ToInt32(Console.ReadLine());
                int b = Convert.ToInt32(Console.ReadLine());

                switch (c)
                {
                    case 'A':
                    case 'a':
                        {
                            d = add(a, b);
                            Console.WriteLine(d);
                            break;


                        }


                    case 'S':
                    case 's':
                        {
                            d = sub(a, b);
                            Console.WriteLine(d);
                            break;
                        }

                    case 'M':
                    case 'm':
                        {
                            d = mul(a, b);
                            Console.WriteLine(d);
                            break;
                        }

                    case 'D':
                    case 'd':
                        {
                            d = div(a, b);
                            Console.WriteLine(d);
                            break;
                        }


                    default:
                        {

                            Console.WriteLine("Please Enter the correct Character");
                            break;
                        }



                }
            }
        }
            private static int add(int a, int b)
    {

                   return a + b;
    }
               private static int sub(int a, int b)
    {

                   return a - b;
    }
               private static int mul(int a, int b)
    {
                   return a * b;
    }
               private static int div(int a, int b)
    {

                   return a / b;
    }



        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:35:45+00:00Added an answer on June 9, 2026 at 4:35 pm

    maybe it is because your application waiting for 2 numbers after you press “q”?

    try to change your code, remove case “q” and replace:

    char c = Convert.ToChar(Console.ReadLine());
    

    with the following:

    char c = Convert.ToChar(Console.ReadLine());
    if(c.ToLower() == 'q')
    {
        Environment.Exit(0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While I am searching for better method to exit a Swing Application in between
I know system.exit(0) should not be used. I have read plenty of tutorials as
I'm currently working on a small console application. This application is based on a
My problem is that memcache logic inside my cakePHP application is not working on
I've been working with VBA for quite a while now, but I'm still not
While I was trying to uplaod file using php on windows machine its working
What are the default values that can be passed to Environment.Exit method and what
I'd like the aspect to exit a method invocation based on a condition like
I want to exit the while loop when the user enters 'N' or 'n'.
How to exit from my application? How to add, option and exit option in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.