2
votes

Sorry, I'm completely new to the programming world. I'm creating a console application that basically takes your input on whether you want to convert Celsius to Fahrenheit or vice-versa.

The problem I'm running into is:

"Error 3 A local variable named 'choice' cannot be declared in this scope because it would give a different meaning to 'choice', which is already used in a 'parent or current' scope to denote something else"

I tried looking at other examples, but they were far more complicated than my brain can understand for now.

namespace TemperatureApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int choice;

            do
            {
                Console.WriteLine("Hi! This is a temperatue app");
                Console.WriteLine("Press 1 for C to F or 2 for F to C");
                //take the user input
                int choice = Convert.ToInt32(Console.ReadLine());


                if (choice == 1)
                {
                    Console.WriteLine("Great, you chose C to F. Now enter the temp.");
                    int celcius = Convert.ToInt32(Console.ReadLine());
                    //next line use the formula and show answer
                }
                if (choice == 2)
                {
                    Console.WriteLine("Great, you chose F to C. Now enter the temp.");
                    int fahrenheit = Convert.ToInt32(Console.ReadLine());
                    //next line use the formula and show answer
                }
                else
                    Console.WriteLine("That is not the right choice.");
                    //In this way, keep asking the person for either 1 or two

            }
            while (choice != 1 || choice != 2);

            Console.ReadLine();
        }
    }
}
5
Is it failing here: if (choice == 2)? - Brian

5 Answers

6
votes
int choice = Convert.ToInt32(Console.ReadLine());

should read

choice = Convert.ToInt32(Console.ReadLine());

By putting the int in the second time, you are declaring another variable called "choice" inside the {}. That definition is conflicting with the one just outside.

1
votes

It's all in the error message, you have declared your choice variable twice within the same scope.

When you read the value from the console input you don't need to re-declare the variable you just need set it. To elaborate:

int choice; // declare variable

do
{
    int choice = ...; // re-declare variable
}

On the second line you want to assign the variable a value, not re-declare it, to do that you just need to replace this line:

int choice = ...

With

choice = ...
1
votes

The problem I'm running into is:

"A local variable named 'choice' cannot be declared in this scope because it would give a different meaning to 'choice', which is already used in a 'parent or current' scope to denote something else".

I tried looking at other examples

You shouldn't look at examples, but at the error and your code. The C# compiler is quite user-friendly with its messages, and this one is quite clear also. You just have to understand the language used.

A local variable named 'choice' cannot be declared

You declare a variable like this: variable-type variable-name, which happens on the line of the error, within your do-loop:

int choice = Convert.ToInt32(Console.ReadLine());

Here with int you declare the variable, int being its type. Removing int will make that line an assignment, which is what you need. The result of Convert.ToInt32(Console.ReadLine()) is assigned (=) to the choice-variable. You can only assign a variable after declaring it, but you have already done that earlier in the code. So the error says:

because it would give a different meaning to 'choice', which is already used in a 'parent or current' scope to denote something else

And if you look up in your code, you see that in a parent scope (in static void Main()) a variable with the same name is declared:

int choice;

Now you should know you only need to declare a variable once, and that this is usually, preferably done in a scope as small as possible. In your case, you can simply remove the int before int choice = Convert.ToInt32(Console.ReadLine());, because you also need to access choice outside the do-loop.

0
votes

Try taking the choice variable declaration out of your cycle scope. Like this:

   int choice;

   do
    {
        Console.WriteLine("Hi! This is a temperatue app");
        Console.WriteLine("Press 1 for C to F or 2 for F to C");
        //take the user input
        choice = Convert.ToInt32(Console.ReadLine());


        if (choice == 1)
        {
            Console.WriteLine("Great, you chose C to F. Now enter the temp.");
            int celcius = Convert.ToInt32(Console.ReadLine());
            //next line use the formula and show answer
        }
        if (choice == 2)
        {
            Console.WriteLine("Great, you chose F to C. Now enter the temp.");
            int fahrenheit = Convert.ToInt32(Console.ReadLine());
            //next line use the formula and show answer
        }
        else
            Console.WriteLine("That is not the right choice.");
            //In this way, keep asking the person for either 1 or two

    }
    while (choice != 1 || choice != 2);

    Console.ReadLine();
}

}

0
votes

You can't have a local variable named the same name as another local variable. You can have a local variable with the same name as a variable of a larger, non-local scope (class member for instance) but in that case the local variable hides the larger-scoped variable.

What you want to do is take the int off the second choice. You don't need to re-declare it, you just want to assign it.

choice = Convert.ToInt32(Console.ReadLine());