4 Answers
+ 2
One of the mistakes that I found is "string = operator_1" there shouldn't be an equal sign there,
I think the code that you intended to do is this:
int number_1 = Convert.ToInt32(Console.ReadLine());
string operator_1 = Console.ReadLine().Trim();
int number_2 = Convert.ToInt32(Console.ReadLine());
if ( operator_1 == "+" || operator_1.ToLower() == "add" )
Console.WriteLine(number_1 + number_2);
else
Console.WriteLine("Error in the operation.");
/*
Input:
10
+
2
*/
+ 1
SoloProg already made it good but I just wanted to Show the option with "Console.ReadKey". First Console.ReadLine is still better for such Things but Here the code:
//Make variables
Int num1 = Convert.ToInt32(Console.ReadLine());
Int num2 = Convert.ToInt32(Console.ReadLine());
//Make a string sum for to give out the sum (or Error)
//(Shortened if-else statment)
string sum = Console.ReadKey().KeyChar == '+' ? num1 + num2 : "Error: No valid Operator";
//Clear the console because ReadKey does Not end the Line
Console.Clear();
//Give out sum
Console.WriteLine(sum);
Code with Console.ReadLine:
...
string sum = Console.ReadLine()== "+"? num1 + num2 : "Error: No valid Operator";
Console.WriteLine(sum);
0
int number_1 = Convert.ToInt32(Console.ReadLine());
int number_2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(number_1 + number_2);
/*
Input:
7
3
*/
0
You did not understand me, the line is needed to enter the operator by type '+'