0

i have to build a program that takes the diameter of a pizza and finds how many slices it can hold and the area of each slice, the pizza, it defines the pizza size with constants, but a getting an error with "int numberOfSlice" it is saying use of unassigned local variable, even thought am assigning thought the if statements.

class Program
{
    static void Main(string[] args)
    {
        //declarations of Constans

        const int SMALL_MIN = 12; 
        const int SMALL_MED = 16;
        const int MED_LARGE = 24;
        const int LARGE_XLARGE = 30;
        const int XL_MAX = 36;
        const int SMALL_SLICE = 8;
        const int MED_SLICE = 12;
        const int LARGE_SLICE = 16;
        const int XL_SLICES = 24;
        //declarations of varable
        double pizzaDiameter;
        int numberOfSlices = 0;
        double sliceArea;
        double radius; 
        string userInput = " ";


        Console.WriteLine("Please enter the diameter of your pizza:"); // tell user to input diameter 
        userInput = Console.ReadLine(); // gets userinput
        double.TryParse(userInput, out pizzaDiameter); // see if userinput is vaild


        if (pizzaDiameter >= SMALL_MIN && pizzaDiameter <= XL_MAX) // if in range will continue
        {

            // all the ranges for the pizzas 
            if (pizzaDiameter >= SMALL_MIN && pizzaDiameter < SMALL_MED)
            {
                numberOfSlices = (SMALL_SLICE);
            }

            else if (pizzaDiameter >= SMALL_MED && pizzaDiameter < MED_LARGE)
            {
                numberOfSlices = (MED_SLICE);
            }

            else if (pizzaDiameter >= MED_SLICE && pizzaDiameter < LARGE_XLARGE)
            {
                numberOfSlices = (LARGE_SLICE);
            }

            else if (pizzaDiameter >= LARGE_XLARGE && pizzaDiameter <= XL_MAX)

            {
                numberOfSlices = (XL_SLICES);
            }


            radius = pizzaDiameter / 2; // divides pizzaDiameter to get radius 


            sliceArea = Math.PI * Math.Pow(radius, 2) / numberOfSlices; // gets slice area

            sliceArea = Math.Round(sliceArea, 2); // rounds to 2 places

            // output of resluts 
            Console.WriteLine("\nA diameter of " + pizzaDiameter + " will yield " + numberOfSlices + " slices.");
            Console.WriteLine("\nEach slice will have an area of " + sliceArea + "\".");

            Console.WriteLine("\nPress any key to exit..."); // tells user to end program
            Console.ReadKey(); // readkey to end program


        }



        else // if the diameter was not in range will display this error 
        {
            Console.WriteLine("\nEntry Error ");
            Console.WriteLine("\nPizza must have a diameter in the range of 12\" to 36\" inclusive!");
            Console.WriteLine("please try again");

            Console.WriteLine("\nPress any key to end this application...");// tells user to end program
            Console.ReadKey(); // readkey to end program

        }      






    }
}

}

6
  • You haven't assigned it in the outer else block. Commented Oct 14, 2013 at 4:01
  • 3
    Don't use SHOUTING_CONSTANTS in C#. Case your constants LikeThis. Commented Oct 14, 2013 at 4:08
  • Alright guys i got it, u guys are amazing for replying so quick! Commented Oct 14, 2013 at 4:08
  • 1
    @EricLippert in my style guide thats how i have to name my constants Commented Oct 14, 2013 at 7:28
  • 5
    @SarujanPathmaranjan: My advice to you is that you throw away your style guide and replace it with a copy of Framework Design Guidelines. Commented Oct 14, 2013 at 14:58

4 Answers 4

4

There error tells you everything you need to know. Your numberOfSlices variable isn't properly initialized. It must be assigned a value along every possible code path before you use it.

Try initializing it at the time it's declared:

int numberOfSlices = 0;

Or alternatively, you could construct your if-blocks more carefully to avoid this error:

if (pizzaDiameter <= XL_MAX || pizzaDiameter >= SMALL_MIN)
{
    if (pizzaDiameter >= SMALL_MIN || pizzaDiameter < SMALL_MED)
    {
        numberOfSlices = (SMALL_SLICE);
    }
    ...
    else  // Note, you do not need the final `if` in this block
    {
        numberOfSlices = (XL_SLICES);
    }

    radius = pizzaDiameter/2;

    sliceArea = Math.PI * Math.Pow(radius, 2);

    Console.WriteLine("\nA diameter of " + pizzaDiameter + " will yield." + numberOfSlices + " slices.");
    Console.WriteLine("\nEach slice will have an area of " + sliceArea + "\".");
    Console.WriteLine("\nPress any key to exit...");
    Console.ReadKey();
}
else
{
    Console.WriteLine(" Entry Error ");
    Console.WriteLine("Pizza must have args diameter in the range of 12\" to 36\" inclusive!");
    Console.WriteLine("please try again");
    Console.WriteLine(" /nPress any key to end this application...");
    Console.ReadKey();
}

In this code, numberOfSlices is only used inside if block where it is guaranteed to be assigned a value before it is used.

Sign up to request clarification or add additional context in comments.

Comments

2

The variable is still unassigned in the outer else block:

if (pizzaDiameter <= XL_MAX || pizzaDiameter >= SMALL_MIN)
{
}
else
{
    // unassigned here
}

Comments

1

If if (pizzaDiameter <= XL_MAX || pizzaDiameter >= SMALL_MIN) returns false then numberOfSlices wont get assigned and the below line will fail.

  Console.WriteLine("\nA diameter of " + pizzaDiameter + " will yield." + numberOfSlices + " slices.");

Comments

1

You're getting the error because you've assigned it in only the if block.
What if the condition is not satisfied? In that case, it won't get initialized and hence throwing that error.

The compiler checks for the assignment in every possible code path before the usage of any variable.

What you need to do is,

int numberOfSlices=0; // Or whatever initial value you want to give
if (pizzaDiameter <= XL_MAX || pizzaDiameter >= SMALL_MIN)
{
}
else
{

}
//Then, even if you try to access it here, it won't throw any error.

Note : It isn't initialized by the default value of int (i.e. 0) because it is not a data member of a class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.