-2

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
}
Lennart
  • 9,657
  • 16
  • 68
  • 84
TheDankDex
  • 11
  • 4
  • Are you getting undefined error or `use of unassigned variable` error. They both are different. I am guessing its the latter by looking at your code. All you need to do in that case is initialize so `int total=0`. The reason for this is that you are only assigning value to the variable within the case statement which means it could be left unassigned. An alternative would be to add `default:` case – adityap Oct 30 '18 at 19:17
  • 2
    Tip: To get good answers on this site, and to provide the best example for others experiencing the same problem as you, it is required that you *accurately* describe your issue. Based on the code you have presented and your problem statement, it does not appear that you have done that. Be sure to correctly capture and present either the actual error received, or the actual code that produced the reported error. One or the other is wrong here. – Anthony Pegram Oct 30 '18 at 19:21
  • Possible duplicate of [Why C# local variables must be initialized?](https://stackoverflow.com/questions/4182666/why-c-sharp-local-variables-must-be-initialized) –  Nov 12 '19 at 08:16

3 Answers3

1

Question: What happens if op is ^?

Answer: total is never assigned to. That's an error in C#.

To fix this, either handle the other cases in your switch statement (should be easy, there's only a few hundred thousand cases), or initialize your total variable when declaring it:

int total = 0;
Blindy
  • 65,249
  • 10
  • 91
  • 131
0

I recommend using Nullable integer to start with null value assigned to it, and at the end check if it has value or not to identify whether user entered appropriate operator.

int? total = null;
Hitesh Gaur
  • 362
  • 1
  • 11
0

As Blindy said you need to handle this with either a initial value for variable total or a default value in the switch.

But before that, you really need to think what would be the logical scenario when you try to make an unknown operation between two number.

My simplest solution would look like this:

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;
    default:
        throw new OperatorUnknownException(op);
}

As you can see an exception is thrown when the operator is unknown. Then you need to handle this type of exception in the calling function.