Reputation: 233
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;
}
}
}
Upvotes: 0
Views: 3233
Reputation: 17080
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);
}
Upvotes: 4
Reputation: 18474
This bit of code
char c = Convert.ToChar(Console.ReadLine());
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
Reads 3 lines from the console.
You probably want
char c = Convert.ToChar(Console.ReadLine());
if (c=='Q' or c=='q') return;
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
Upvotes: 0
Reputation: 119
Just change your code like that
bool exitLoop = false;
while(exitLoop) {
...
case 'q':
{
exitLoop = true;
break;
}
...
}
Upvotes: -1
Reputation: 38200
char c = Convert.ToChar(Console.ReadLine());
// you will need to Exit here if char is 'q'
// else it is expecting to read more entries below before it reaches your "case"
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
Upvotes: 1