Holly Marie Batchelor
Holly Marie Batchelor

Reputation: 113

Terminating a c# Console application

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");
}

Upvotes: 1

Views: 4444

Answers (6)

blins
blins

Reputation: 2535

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");
        }
    }
}

Upvotes: 0

Trisped
Trisped

Reputation: 6003

The "press any key to continue" text is added by when you run the project from Visual Studio.

Instead, go to the bin and run it from there.

Make sure you build the project before you run it. If you run from VS it will automatically build if needed, but running the file from windows explorer does not do this.

Upvotes: 0

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236218

If you are starting console application from Visual Studio not in debug mode (Ctrl+F5), then console window stays open after application finished execution. And it prompts Press any key to continue.... Again, at this point your console application has exited.

When you will run *.exe file, then your console will close without asking for key press. That's just Visual Studio 'feature' - it runs cmd.exe telling it to run your executable and then pause.

Here is what Visual Studio does when you start application without debugging (you can copy this to *.bat file and run it)

ECHO OFF
CLS
"Path\To\Your\ConsoleApplication.exe"
PAUSE

Upvotes: 0

Hoang Tram
Hoang Tram

Reputation: 31

I recommend you to try the application.close() function. It has saved my day many times. If it doesnt help, let me know.

Upvotes: 3

Andrew White
Andrew White

Reputation: 874

I would first run your app in debug and check to make sure your Application.Exit is actually getting hit. This may shed some light onto why the app isn't exiting.

You should use Environment.Exit(0). This appears to be a better way to end Console apps.

Source: http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx

If this code is in Main I would recommend using a return; after your call to exit the app.

Upvotes: 1

Alicja Puacz
Alicja Puacz

Reputation: 41

I think what you need to do is to right-click on your project, choose "Properties"and then (quoting msdn.com):

To set this linker option in the Visual Studio development environment

Open the project's Property Pages dialog box. For details, see Setting Visual C++ Project Properties.

Click the Linker folder.

Click the System property page.

Modify the SubSystem property.

For the SubSystem, pick Console. Hope this helps! :)

Upvotes: 4

Related Questions