Reputation: 21
using System;
namespace prac4b
{
class prac4b
{
static void Main(string[] args)
{
int number1, number2, result;
char action;
string tempVal = "";
bool parseAttempt = false;
// ask for first number
Console.ReadLine();
Console.Write("Enter number > ");
//testing if integer with TryParse
tempVal = Console.ReadLine();
parseAttempt = Int32.TryParse(tempVal, out number1);
// if not a number
if (parseAttempt == false)
{
Console.WriteLine("You have not entered a number, application will now exit.");
Environment.Exit(0);
}
//if true, continue, ask for number2
if (parseAttempt == true)
{
//asking for number
Console.Write("Enter another number > ");
tempVal = Console.ReadLine(); //storing number temporailiy for checking
parseAttempt = Int32.TryParse(tempVal, out number2); //checking number2 if integer
//if not a number
if (parseAttempt == false)
{
Console.WriteLine("ERROR you have not entere a valid integer");
Environment.Exit(0);
}
//if true, continue, ask for action(+*-+)
Console.WriteLine("Action (*/+-) > ");
action = Console.ReadLine();
switch (action) //switch statement for action list
{
case "+":
Console.WriteLine("Result is > ", number1 + number2);
break;
case "-":
Console.WriteLine("Result is > ", number1 - number2);
break;
case "*":
Console.WriteLine("Result is > ", number1 * number2);
break;
case "/":
Console.WriteLine("Result is > ", number1 / number1);
break;
default:
Console.WriteLine("ERROR INVALID INPUT");
Environment.Exit(0);
break;
}
Console.WriteLine();
}
}
}
}
I'm trying to get this switch statement to work but it keeps coming up with error, can't change string to char. I don't see where I have tried to change a string to char.
Upvotes: 2
Views: 564
Reputation: 186698
You may find useful to remove swicth
at all and implement a dictionary instead:
Dictionary<String, Func<Double, Double, Double>> Actions = new {
{"+", (x, y) => x + y},
{"-", (x, y) => x - y},
{"*", (x, y) => x * y},
{"/", (x, y) => x / y},
};
...
Console.WriteLine("Action (*/+-) > ");
action = Console.ReadLine();
Func<Double, Double, Double> func;
if (Actions.TryGetValue(action, out func))
Console.WriteLine("Result is > ", func(number1, number2));
else
Console.WriteLine("ERROR INVALID INPUT");
if you have a lot of actions (e.g. power **
, reminder %
etc.) dictionary implemenation is more readable.
Upvotes: 1
Reputation: 45947
you declare
char action;
as a char Type but
switch (action)
{
case "+": // here you compare it with a string.
....
case "-": // here you compare it with a string.
....
case "*": // here you compare it with a string.
....
case "/": // here you compare it with a string.
...
action = Console.ReadLine(); //here you try to set a string
replace char action;
with string action;
Upvotes: 2
Reputation: 134
using System;
namespace prac4b
{
class prac4b
{
static void Main(string[] args)
{
int number1, number2, result;
char action;
string tempVal = "";
bool parseAttempt = false;
// ask for first number
Console.ReadLine();
Console.Write("Enter number > ");
//testing if integer with TryParse
tempVal = Console.ReadLine();
parseAttempt = Int32.TryParse(tempVal, out number1);
// if not a number
if (parseAttempt == false)
{
Console.WriteLine("You have not entered a number, application will now exit.");
Environment.Exit(0);
}
//if true, continue, ask for number2
if (parseAttempt == true)
{
//asking for number
Console.Write("Enter another number > ");
tempVal = Console.ReadLine(); //storing number temporailiy for checking
parseAttempt = Int32.TryParse(tempVal, out number2); //checking number2 if integer
//if not a number
if (parseAttempt == false)
{
Console.WriteLine("ERROR you have not entere a valid integer");
Environment.Exit(0);
}
//if true, continue, ask for action(+*-+)
Console.WriteLine("Action (*/+-) > ");
var readaction = Console.ReadLine();
string actionString = readaction.ToString();
switch (actionString) //switch statement for action list
{
case "+":
Console.WriteLine("Result is > ", number1 + number2);
break;
case "-" :
Console.WriteLine("Result is > ", number1 - number2);
break;
case "*" :
Console.WriteLine("Result is > ", number1 * number2);
break;
case "/" :
Console.WriteLine("Result is > ", number1 / number1);
break;
default :
Console.WriteLine("ERROR INVALID INPUT");
Environment.Exit(0);
break;
}
Console.WriteLine();
}
}
}
}
Upvotes: 0
Reputation: 5157
Use
action = Console.ReadKey().KeyChar;
instead of
action = Console.ReadLine();
&
case "+":
to case '+':
Upvotes: 2