Just a newbie to c# looking for some guidance.
In the below code if I used double area; it would give me the error : "Use of unassigned local variable area". I had to put double area = 0;
My question is why? Why do I have to assign it a value. I have never had to do that before so it's a bit confusing to me.
Thanks in advance for your response.
int userValue, rad, heightOfRectangle, widthOfRectangle, baseOfTriangle, heightOfTriangle;
double area = 0;
Console.Write("\n\n");
Console.Write("Calculating Area of Geometrical Shape\n");
Console.Write("=======================================\n");
Console.Write("\n\n");
Console.Write("Please select 1 for Circle, 2 for Rectangle and 3 for Triangle: ");
userValue = Convert.ToInt32(Console.ReadLine());
switch (userValue)
{
case 1:
Console.Write("Please Enter Radius of Circle: ");
rad = Convert.ToInt32(Console.ReadLine());
area = 3.14 * rad * rad;
break;
case 2:
Console.Write("Please enter Height of Rectangle: ");
heightOfRectangle = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter Width of Rectangle: ");
widthOfRectangle = Convert.ToInt32(Console.ReadLine());
area = widthOfRectangle * heightOfRectangle;
break;
case 3:
Console.Write("Please enter Base of Triangle: ");
baseOfTriangle = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter height of Triangle: ");
heightOfTriangle = Convert.ToInt32(Console.ReadLine());
area = .5 * baseOfTriangle * heightOfTriangle;
break;
}
Console.WriteLine("\nThe area is {0}", area);