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