2

I'm new to writing codes, I understand a few but I can't make some things work.

I want this to make the user choose a category first before giving them access to one but I get the error saying "use of the unassigned local variable". Did I do something wrong?

    public static void Main(string[] args)

    {
        bool CalculateSavings, Grades;
        if (CalculateSavings == true)
        {
            int MySavings, Allowance, Food, Transportation, WeeklySavings, MonthlySavings;

            Console.WriteLine("Input your daily allowance:");
            Allowance = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Input your daily food expenses:");
            Food = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Input your daily transportation expenses:");
            Transportation = Convert.ToInt32(Console.ReadLine());

            {

                MySavings = Allowance - (Food + Transportation);
                Console.WriteLine("Your calculated daily savings: {0}", MySavings);

                WeeklySavings = MySavings * 5;
                Console.WriteLine("Your calculated weekly savings: {0}", WeeklySavings);

                MonthlySavings = WeeklySavings * 4;
                Console.WriteLine("Your calculated monthly savings: {0}", MonthlySavings);

                Console.ReadKey();
            }


        }
        else if (Grades == true)
        {
            double Filipino, English, Science, Math, History, Average;

            Console.WriteLine("Input grade in Filipino subject:");
            Filipino = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Input grade in English subject:");
            English = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Input grade in Science subject:");
            Science = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Input grade in Math subject:");
            Math = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Input grade in History subject:");
            History = Convert.ToDouble(Console.ReadLine());

            Average = (Filipino + English + Science + Math + History) / 5;
            Console.WriteLine("Overall grade average: {0}", Average);
            Console.ReadLine();

        }
    }
}

}

Make the user choose a category first before giving them access to one but I get the error saying "use of the unassigned local variable".

Miss Silence
  • 21
  • 1
  • 5
  • 4
    You're using `CalculateSavings` in the statement `if (CalculateSavings == true)` and `Grades` in the statement `else if (Grades == true)` but neither of them have been initialized, only declared. – Patrick Roberts Aug 02 '19 at 15:24
  • 1
    Which line throws the error? – Sam Axe Aug 02 '19 at 15:24
  • 2
    You declare the bool variables `bool CalculateSavings,Grades` but you don't initialize them. Well, since these are value types they are initialized by the runtime. But the compiler wants to protect you from careless mistakes because he knows for sure that you haven't assigned a value before usage. – Tim Schmelter Aug 02 '19 at 15:24
  • 1
    You **always** have to initialize the value before you use it. This applies from the lowest Assembler level to .NET. More "to the metal" languages" will just blindly take wichever bits happen to be written in those memory locations (from whatever has written there last), but the .NET Compiler is way more proactive in preventing issues. – Christopher Aug 02 '19 at 15:28

4 Answers4

2

You need to set your variables. Like:

bool CalculateSavings = true;
bool Grades = true;

C# compiler doesn't allow unassigned variables because of avoid possible errors.

  • Lacking the requested explanation why the compiler complains. Maybe OP thinks that a `bool` is `false` by default. Apart from the fact that this would just hide a bug because the `Grades`-block will never be entered. These bools should be determined by user input. – Tim Schmelter Aug 02 '19 at 15:34
  • 1
    If this was the solution, what would be the point of having them in the first place? The conditional logic could just be removed entirely. I don't think the question really has enough information currently to provide a complete answer. – Patrick Roberts Aug 02 '19 at 15:36
0

In code you appended to the question, you define variables with bool CalculateSavings, Grades;, but you do not assign them (like bool myVariable = true).

And right after that you try to use them ((CalculateSavings == true)) , but how C# should compare and use them?

So, to fix your problem, you need to assign them with true or false for bool.

Unassigned variables are like: you need toilet paper, you know what it is, but you just dont have it at all. (but not null).

misticos
  • 718
  • 5
  • 22
0

You should also write if else statement like following example

  if (CalculateSavings){
            // your Code
        }else if (Grades){  
     // your Code
   }

Also you need to assign values to variables.

bool CalculateSavings = true;
bool Grades = true;

Due to unassigned variable c# its give errors.

Bhaumik Shah
  • 382
  • 2
  • 8
  • 20
0

You could possibly draw an analogy to storage boxes being variables.

That is, you were given two boxes from the attic, so you label them CalculateSavings and Grades. The next thing you want is to choose a path depending on what's in them.

What the compiler is telling you is that there is no way to be sure what's in them (there could've been something stored in those boxes when taken from the attic). Therefor the content is unreliable. The compiler does not automatically clean and initialize local variables, it rather gives you the opportunity to choose what that value should be.

So putting an initial value when declaring a local variable would satisfy that concern. It then know what's in a box and can safely continue.

bool CalculateSavings = false, Grades = true; // An example of initialized variables.
Rob
  • 181
  • 5