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;
}
}
}
maybe it is because your application waiting for 2 numbers after you press “q”?
try to change your code, remove case “q” and replace:
with the following: