C# Beginner - "Use of Unassigned Local Variable" Issue

1.8k views Asked by At

New to C# (only coding for a week so far) trying to create a practice program. Can't seem to get the data I want stored in 'price1' and 'price2'. Error is CS0165 Use of unassigned local variable 'price1' and 'price2'.

I've tried moving lines of code around and adding in a return command, but I can't quite seem to figure it out.

        Console.Write("What grocery are you buying: ");
        string product1 = Console.ReadLine();
        Console.Write("How many are you buying: ");
        int quantity1 = Convert.ToInt32(Console.ReadLine());

        double price1;
        if (product1 == "Steak")
        {
            price1 = Convert.ToDouble(steak.price * quantity1);
        }
        if (product1 == "Cheerios")
        {
            price1 = Convert.ToDouble(cheerios.price * quantity1);
        }
        if (product1 == "Pepsi")
        {
            price1 = Convert.ToDouble(pepsi.price * quantity1);
        }
        if (product1 == "Celeste Pizza")
        {
            price1 = Convert.ToDouble(celeste.price * quantity1);
        }



        Console.Write("What second grocery are you buying: ");
        string product2 = Console.ReadLine();
        Console.Write("How many are you buying: ");
        int quantity2 = Convert.ToInt32(Console.ReadLine());

        double price2;
        if (product2 == "Steak")
        {
            price2 = Convert.ToDouble(steak.price * quantity2);
        }
        if (product1 == "Cheerios")
        {
            price2 = Convert.ToDouble(cheerios.price * quantity2);
        }
        if (product1 == "Pepsi")
        {
            price2 = Convert.ToDouble(pepsi.price * quantity2);
        }
        if (product1 == "Celeste Pizza")
        {
            price2 = Convert.ToDouble(celeste.price * quantity2);
        }

        Console.WriteLine(price1 + price2);

Trying to get data stored in 'price1' and 'price2' so I can add them together at the end. Sorry if I'm getting any terminology wrong here.

2

There are 2 answers

1
ADyson On BEST ANSWER

The problem is that if product1 does not equal any of the values in your if statements, then none of those sections will ever run, and so in theory there is a danger that price1 might never be given a value. And it can't use something which doesn't have a value to add it to something else. That's what the compiler is complaining about. You need to give price1 a default value when you first declare it, as a fallback option in case the user enters something which is not one of the four expected product names.

double price1 = 0;

Is probably ok for this scenario, but you can choose whatever value you think is best, as long as there is some kind of value.

You will have the exact same issue with price2 as well.

1
Circa On

Need to initialize "local variables" price1 and price2 to default values of your choice probably being 0.

Essentially, it's not guaranteed that price1 or price 2 will be set to anything when you decide to get their sum and display it.