0

I'm tasked with making a program that will take an order for a pizza. Ive given the user two radio buttons to select the size (large 6$, X-Large $12) and four radio buttons for the number of toppings (1, 2, 3 ,4). I came up with an equation that will calculate the the cost with tax included (C=1.13(0.75(x-1)+1)+S, x being the toppings and S being the size).

My problem is that once I try to code the equation, it says that there is an error with the Use of Unassigned variables numbToppings and size in the second last line.

Any ideas on why this is happening and how to fix it.

        const double taxes = 1.13;
        const double toppings = 0.75;
        double size;
        double numToppings;
        double costInitial;
        double costTotal;

        if (radLarge.Checked == true)
        {
            size = 6;
        }

        else if (radXLarge.Checked == true)
        {
            size = 12;
        }

        if (rad1.Checked == true)
        {
            numToppings = 1;
        }

        else if (rad2.Checked == true)
        {
            numToppings = 2;
        }

        else if (rad3.Checked == true) 
        {
            numToppings = 3;
        }

        else if (rad4.Checked == true) 
        {
            numToppings = 4;
        }

        costInitial = ((toppings * numToppings - 1) + 1) + size;
        costTotal = taxes * costInitial; 

1 Answers1

0

Simple do this

double numToppings = 0;

Set it to zero.

Reason: you are doing some calculations on numToppings and compiler is not intelligent enough to ascertain that you have handled all possible conditions and numToppings will be initialized anyway.

For more read this Strange behaviour of switch case with boolean value.

Community
  • 1
  • 1
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208