I'm very new in c# and i'm trying to do a simple calculator.
However when I write Console.WriteLine(total)
, I get a compile-time error:
Use of unassigned local variable 'total'
Local variable 'total' might not be initialized before accessing
here's the code:
static void Main(string[] args)
{
Console.WriteLine("write a number:");
int num_one = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("write a operator: + ; - ; * ; /");
string op = Console.ReadLine();
Console.WriteLine("write a second number:");
int num_two = Convert.ToInt32(Console.ReadLine());
int total;
switch (op)
{
case "+":
total = num_one + num_two;
break;
case "-":
total = num_one - num_two;
break;
case "*":
total = num_one * num_two;
break;
case "/":
total = num_one / num_two;
break;
}
Console.WriteLine(total); // <-- this line gives a compile-time error
}