-4

I am trying to build a very easy calculator on C#, I would like to store the result on the variable "result" and display it by means of Console.WriteLine(result). For some reason Console.WriteLine(result) is not allowed.

using System;

namespace Giraffe
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("insert number1");
            double num1 = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("insert operator");
            string op = Console.ReadLine();

            Console.WriteLine("insert number2");
            double num2 = Convert.ToDouble(Console.ReadLine());
         
            if (op == "+")
            {
                double result = num1 + num2;
            }

            else if(op == "-")
            {
                double result = num1 - num2;
            }

            else if (op == "*")
            {
                double result = num1 * num2;
            }

            else if (op == "/")
            {
                double result = num1 / num2;
            }


            Console.WriteLine(result);


        }
     
    }
}

Thanks a lot

  • 3
    Hint: you're declaring four separate local variables each called `result`, none of which is available outside the block it's declared in. I suspect you want *one* variable, declared just before the `if` statement, and assigned a value in each of the `if` statement bodies. You should also work out what you want the result to be if `op` isn't one of the cases you've coded for. – Jon Skeet Mar 14 '21 at 12:55
  • 3
    seems you might wanna read up on Variable Scope (maybe [this tutorial](https://learn.microsoft.com/en-us/learn/modules/csharp-code-blocks/) will help). the TLDR is: since you declared your `double result` _inside_your if-blocks, it basically stops existing after you leave the block. you have to move the declaration outside. – Franz Gleichmann Mar 14 '21 at 12:56
  • 1
    Does this answer your question? [Variable scope confusion in C#](https://stackoverflow.com/questions/1196941/variable-scope-confusion-in-c-sharp) – Progman Mar 14 '21 at 12:57

1 Answers1

2

Declare double result outside if statement

static void Main(string[] args)
    {

        Console.WriteLine("insert number1");
        double num1 = Convert.ToDouble(Console.ReadLine());

        Console.WriteLine("insert operator");
        string op = Console.ReadLine();

        Console.WriteLine("insert number2");
        double num2 = Convert.ToDouble(Console.ReadLine());

        double result = 0;

        if (op == "+")
            result = num1 + num2;
        else if (op == "-")
            result = num1 - num2;
        else if (op == "*")
            result = num1 * num2;
        else if (op == "/")
            result = num1 / num2;
        Console.WriteLine(result);
    }
Vít Bednář
  • 288
  • 2
  • 10
  • 1
    Yes, this is correct, but does it help the OP to understand its problem? You can add some lines to explain why one needs to declare the variable in that position. – Steve Mar 14 '21 at 13:29