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 8851751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:13:57+00:00 2026-06-14T13:13:57+00:00

I am trying to close my c# console application (running without debugging) and nothing

  • 0

I am trying to close my c# console application (running without debugging) and nothing is working… i have tried all the usual suspects with different exit codes but nothing is terminating it =/ is it because the option is in a bunch of nested if statements? Its probably something really simple i’m missing but its hurting my brain now someone help please! I’ve tried :

System.Environment.Exit(0);
System.Environment.Exit(1);
System.Environment.Exit(-1);
return;
Application.Exit(); //(wont even except it)

if context helps i have used nested if statements to check if the user has inputted a number or the letter ‘q’ if they have inputted a number a calculation is carried out, if the have entered the letter q then the program is to exit and for anything else error statements are outputted.

string userInput;
int userInputDigit = 0;
double userCost = 0;
char userInputChar;

userInput = Convert.ToString(Console.ReadLine());

if (int.TryParse(userInput, out userInputDigit))
{
    if (userInputDigit <= 50)
    {
        userCost = (price * userInputDigit);
        Console.WriteLine("You have purchased {0} Widgets at a cost of {1:c0}", userInputDigit, userCost);
    }
    else if ((userInputDigit > 50) && (userInputDigit <= 80))
    {
        userCost = (price * 50) + ((userInputDigit - 50) * (price - 1));
        Console.WriteLine("You have purchased {0} Widgets at a cost of {1:c0}", userInputDigit, userCost);
    }
    else if ((userInputDigit > 80) && (userInputDigit <= 100))
    {
        userCost = (price * 50) + (30 * (price - 1)) + ((userInputDigit - 80) * (price - 2.50));
        Console.WriteLine("You have purchased {0} Widgets at a cost of {1:c0}", userInputDigit, userCost);
    }
    else
    {
        Console.WriteLine("Error! Please input a number between 0 and 100");
    }

}
else if (char.TryParse(userInput, out userInputChar))
{
    if ((userInput == "q") || (userInput == "Q"))
    {
        System.Environment.Exit(0);
    }
    else
    {
        Console.WriteLine("Incorrect Letter Inputted");
    }
}
else
{
    Console.WriteLine("Error! Please input a number or 'q' to quit");
}
  • 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-14T13:13:58+00:00Added an answer on June 14, 2026 at 1:13 pm

    Use a loop and just let Main return normally. Furthermore, I also tried to simplify the condition checking a bit along with the string comparison and parsing. Your error message suggests a validation range (“between” 0 and 100) that is not actually enforced by the preceding if/else if logic. For instance your first case (… <= 50) would be true if user enters a negative value. Also, I did not see where price was ever declared so I made up a constant in my example.

    static bool ExitRequired(string line)
    {
        return string.Equals(line, "q", StringComparison.OrdinalIgnoreCase);
    }
    
    static void Main(string[] args)
    {
        const double price = 10;
        int userInputDigit;
        double userCost;
        string line = null;
    
        while (!ExitRequired(line))
        {
            Console.WriteLine("Enter a number or press 'q' to exit...");
            line = Console.ReadLine();
    
            if (ExitRequired(line))
                break;
    
            if (int.TryParse(line, out userInputDigit) 
                && userInputDigit > 0 
                && userInputDigit < 100)
            {
                if (userInputDigit <= 50)
                {
                    userCost = (price * userInputDigit);
                    Console.WriteLine("You have purchased {0} Widgets at a cost of {1:c0}", userInputDigit, userCost);
                }
                else if ((userInputDigit > 50) && (userInputDigit <= 80))
                {
                    userCost = (price * 50) + ((userInputDigit - 50) * (price - 1));
                    Console.WriteLine("You have purchased {0} Widgets at a cost of {1:c0}", userInputDigit, userCost);
                }
                else if ((userInputDigit > 80) && (userInputDigit <= 100))
                {
                    userCost = (price * 50) + (30 * (price - 1)) + ((userInputDigit - 80) * (price - 2.50));
                    Console.WriteLine("You have purchased {0} Widgets at a cost of {1:c0}", userInputDigit, userCost);
                }
            }
            else
            {
                Console.WriteLine("Error! Please input a number between 0 and 100");
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a test console application, where I am trying to get serializaiton and
All, I've got the following JS Fiddle: http://jsfiddle.net/HHtBX/11/ I'm trying to basically close all
I know about error in application running on server and invoked operation disconnect all
I am trying to create a simple console based java application, which requires users
I'm trying to run a Console Application (connect and read SQL), but getting error
I am writing a C# console application and am trying to check when my
I built a console application and I'm trying to test if my application works
I am trying to implement some proper NHibernate session management in my console application,
I am trying to close a memcached connection to start a new one. But
I'm trying to close the distance between the checkboxes and the labels. I would

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.