How do I return a string from a Int method? Please see my code below. In the last method I would like to print the string "Invalid Operator" if the use keyed in a wrong operator
static void Main(string[] args)
{
Console.WriteLine("Welcome to the simple calculator");
Console.WriteLine("-------------");
Console.WriteLine("Enter the first number");
int num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the Second number");
int num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Select the Operator");
Console.WriteLine("Key in on of the following numbers:\n" + "1 to add \n" + "2 to subtract \n" + "3 to multiply \n" + "4 to divide");
int op = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Your result is");
Console.WriteLine("---------------");
Console.WriteLine(GetResult(num1, num2, op));
Console.ReadLine();
}
I would like the below method to return both the String and Int values
static int GetResult(int a, int b, int c)
{
int result;
if (c == 1)
{
result = a + b;
}
else if (c == 2)
{
result = a - b;
}
else if (c == 3)
{
result = a * b;
}
else if (c == 4)
{
result = a / b;
}
else
{
result = "Invalid Operator";
}
return result;
}
ValueTuplestatic string GetResult(int a, int b, int c, out rersult). Then you could print your string result while also getting the numerical value. Although the use case doesn't make sense, hopefully there's a bigger purpose.