0

Its probably a very easy solution but when it gives 3 error messages:

The name 'iNum1' does not exist in the current context  
The name 'iNum2' does not exist in the current context  
The name 'soOper' does not exist in the current context 

When I remove my last line of code it works but without it I can't calculate it. I hope someone can help. Here is the code.

//information
    Console.WriteLine("This is a calculator");

    //Let them fill in the first number
    Console.WriteLine("Please enter the first number");
    bool bNoNum1 = true;
    while (bNoNum1)
    {

        string sNum1 = Console.ReadLine();

        try
        {
            int iNum1 = int.Parse(sNum1);
            bNoNum1 = false;
        }

        catch (Exception)
        {
            Console.WriteLine("That's not a number");
        }

    }



    //Let them fill in (*, +, / of -)
    Console.WriteLine("Please enter +, +, - or :");

    bool bNoOperator = true;


    do
    {
        string sOper = Console.ReadLine();

        if (sOper == "x")
        {
            string soOper = "*";
            bNoOperator = false;
        }
        else if (sOper == ":")
        {
            string soOper = "/";
            bNoOperator = false;
        }
        else if (sOper == "+")
        {
            string soOper = "+";
            bNoOperator = false;
        }
        else if (sOper == "-")
        {
            string soOper = "-";
            bNoOperator = false;
        }
        else
        {
            Console.WriteLine("De operator " + sOper + " Is niet bekend. Kies uit +, -, x of :");
        }
    } while (bNoOperator);


    //Enter second number
    Console.WriteLine("Please enter the second number");

    bool bNoNum2 = true;
    while (bNoNum2)
    {
        string sNum2 = Console.ReadLine();

        try
        {
            int iNum2 = int.Parse(sNum2);
            bNoNum2 = false;
        }

        catch (Exception)
        {
            Console.WriteLine("That's not a number");
        }
    }

    //calculating

    int uitkomst = iNum1 + soOper + iNum2;
Bacteria
  • 8,406
  • 10
  • 50
  • 67
HaPo
  • 19
  • 1
  • 1
    related: http://stackoverflow.com/questions/2693138/variable-scope-in-statement-blocks – rene Sep 20 '15 at 13:07
  • 1
    You're not the first one to run into that problem: http://stackoverflow.com/search?q=does+not+exist+in+the+current+context+%5Bc%23%5D – rene Sep 20 '15 at 13:12
  • 1
    possible duplicate of [C# Math calculator](http://stackoverflow.com/questions/2859111/c-sharp-math-calculator) – Luke Sep 20 '15 at 13:27

2 Answers2

2

You need to declare those 3 variables as a global one's outside your context, put those variables above the line " like this,

Console.WriteLine("This is a calculator");
"
int iNum1;
int iNum2;
string sOper = "";
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0

You declare iNum1 and iNum2 at the wrong place - inside some inner brackets. They are not known at the scope where the last line lives. Declare those variables at a different level.

In any case when you will do that you will have another issue: soOper is a string. You are adding an int with a string and with another int.

Ladi
  • 1,274
  • 9
  • 17