-2

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.

TheXoy
  • 3
  • 2
  • 1
    By the time the code gets to the line `What second grocery are you buying`, is it *guaranteed* to have a value assigned to `price1`? You have four `if` statements that might assign a value, but is it possible none of those four conditions match? That's why you're getting this error. You cannot use a variable's value before that variable has been given a value. –  May 20 '19 at 22:16
  • Would be helpful if you included the entire .cs file to save time. – Philip Brack May 20 '19 at 22:24
  • 1
    FYI, placing i am a beginner ect ect ect in your question will never help. We don't care about your level of knowledge, we only care if they are well worded and thought through, formatted, and not duplicates – TheGeneral May 20 '19 at 23:55

2 Answers2

0

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.

ADyson
  • 57,178
  • 14
  • 51
  • 63
0

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.

Circa
  • 11