I am teaching myself C# and the current chapter challenge asked me to:
Take your last project and create additional methods that subtract, multiply or divide the two numbers passed into it. In the divide method, check that the second number is not 0, since dividing by 0 is an illegal mathematical concept. If the second number is a 0, just return back a 0.
Now I wrote the below which I believe satisfies all the criteria. I wasn’t sure if an IF statement was the best choice but it did work. I also thought that a SWITCH would have also done the trick.
So first question, would IF or SWITCH have been better?
Second question. In the ELSE, I give a generic failure message if the user does not select one of the available options, but what I would like to do is if the ELSE is called (Not sure what the correct term is), I want the program to go back to the start and ask the user to try again and display the first Console.Writeline() asking to choose an Operator. I know this isn’t part of the challenge, but it seems like a logical addition to the program and would like to know if this is possible without resorting to anything too complicated.
Thanks in advance!
string whichOp;
int firstNum, secondNum, result;
Console.WriteLine("What Operator do you wish to use? [A]dd, [S]ubtract, [M]ultiply or [D]ivide?");
whichOp = Console.ReadLine();
whichOp = whichOp.ToLower();
if (whichOp == "a")
{
Console.Write("You chose Addition. Please choose your first number: ");
firstNum = int.Parse(Console.ReadLine());
Console.Write("You chose the number {0}. Please choose a second number: ", firstNum);
secondNum = int.Parse(Console.ReadLine());
result = Add(firstNum, secondNum);
Console.WriteLine("You chose the number {0}. {1} plus {2} equals {3}.", secondNum, firstNum, secondNum, result);
}
else if (whichOp == "s")
{
Console.Write("You chose Subtraction. Please choose your first number: ");
firstNum = int.Parse(Console.ReadLine());
Console.Write("You chose the number {0}. Please choose a second number: ", firstNum);
secondNum = int.Parse(Console.ReadLine());
result = Sub(firstNum, secondNum);
Console.WriteLine("You chose the number {0}. {1} minus {2} equals {3}.", secondNum, firstNum, secondNum, result);
}
else if (whichOp == "m")
{
Console.Write("You chose Multiplication. Please choose your first number: ");
firstNum = int.Parse(Console.ReadLine());
Console.Write("You chose the number {0}. Please choose a second number: ", firstNum);
secondNum = int.Parse(Console.ReadLine());
result = Mult(firstNum, secondNum);
Console.WriteLine("You chose the number {0}. {1} times {2} equals {3}.", secondNum, firstNum, secondNum, result);
}
else if (whichOp == "d")
{
Console.Write("You chose Division. Please choose your first number: ");
firstNum = int.Parse(Console.ReadLine());
Console.Write("You chose the number {0}. Please choose a second number: ", firstNum);
secondNum = int.Parse(Console.ReadLine());
result = Div(firstNum, secondNum);
Console.WriteLine("You chose the number {0}. {1} divided by {2} equals {3}.", secondNum, firstNum, secondNum, result);
}
else
Console.WriteLine("FAIL! You did not choose an available option.");
Console.ReadLine();
}
static int Add(int num1, int num2)
{
int theAnswer;
theAnswer = num1 + num2;
return theAnswer;
}
static int Mult(int num1, int num2)
{
int theAnswer;
theAnswer = num1 * num2;
return theAnswer;
}
static int Sub(int num1, int num2)
{
int theAnswer;
theAnswer = num1 - num2;
return theAnswer;
}
static int Div(int num1, int num2)
{
int theAnswer;
if (num2 == 0)
return 0;
theAnswer = num1 / num2;
return theAnswer;
}
EDIT: I took the suggestions of those here and rebuilt the program with SWITCH and WHILE. Someone also said that because a lot of the code is the same I should be able to reuse it. I like that idea and will look into how I can do that.
var retry = true;
while (retry)
{
retry = false;
string whichOp;
int firstNum, secondNum, result;
Console.WriteLine("What Operator do you wish to use? [A]dd, [S]ubtract, [M]ultiply or [D]ivide?");
whichOp = Console.ReadLine();
whichOp = whichOp.ToLower();
switch (whichOp)
{
case "a":
Console.Write("You chose Addition. Please choose your first number: ");
firstNum = int.Parse(Console.ReadLine());
Console.Write("You chose the number {0}. Please choose a second number: ", firstNum);
secondNum = int.Parse(Console.ReadLine());
result = Add(firstNum, secondNum);
Console.WriteLine("You chose the number {0}. {1} plus {2} equals {3}.", secondNum, firstNum, secondNum, result);
Console.ReadLine();
break;
case "s":
Console.Write("You chose Subtraction. Please choose your first number: ");
firstNum = int.Parse(Console.ReadLine());
Console.Write("You chose the number {0}. Please choose a second number: ", firstNum);
secondNum = int.Parse(Console.ReadLine());
result = Sub(firstNum, secondNum);
Console.WriteLine("You chose the number {0}. {1} minus {2} equals {3}.", secondNum, firstNum, secondNum, result);
Console.ReadLine();
break;
case "m":
Console.Write("You chose Multiplication. Please choose your first number: ");
firstNum = int.Parse(Console.ReadLine());
Console.Write("You chose the number {0}. Please choose a second number: ", firstNum);
secondNum = int.Parse(Console.ReadLine());
result = Mult(firstNum, secondNum);
Console.WriteLine("You chose the number {0}. {1} times {2} equals {3}.", secondNum, firstNum, secondNum, result);
Console.ReadLine();
break;
case "d":
Console.Write("You chose Division. Please choose your first number: ");
firstNum = int.Parse(Console.ReadLine());
Console.Write("You chose the number {0}. Please choose a second number: ", firstNum);
secondNum = int.Parse(Console.ReadLine());
result = Div(firstNum, secondNum);
Console.WriteLine("You chose the number {0}. {1} divided by {2} equals {3}.", secondNum, firstNum, secondNum, result);
Console.ReadLine();
break;
default:
Console.WriteLine("I'm sorry. {0} is not an available option. Please try again.", whichOp.ToUpper());
retry = true;
break;
}
}
}
static int Add(int num1, int num2)
{
int theAnswer;
theAnswer = num1 + num2;
return theAnswer;
}
static int Mult(int num1, int num2)
{
int theAnswer;
theAnswer = num1 * num2;
return theAnswer;
}
static int Sub(int num1, int num2)
{
int theAnswer;
theAnswer = num1 - num2;
return theAnswer;
}
static int Div(int num1, int num2)
{
int theAnswer;
if (num2 == 0)
return 0;
theAnswer = num1 / num2;
return theAnswer;
}
(1) switch is often a cleaner way to express this kind of branching. The major limitation of switch is that it only works on compile-time constant values (const variables, literal strings, ints, enums, etc.). In this case, that doesn’t seem to be an issue, so switch would probably be a good choice for cleaner, slightly shorter code. In performance-sensitive code (which this is certainly not), a single switch can be faster than a bunch of ifs because the program will test the value and jump straight to the right case rather than testing against each if condition until reaching a match.
(2) an easy way to do this would be to wrap your entire program in a loop: