Maha

Maha

  • 0
  • 0
  • 257.1k

Unassigned local variable

Dec 24 2011 4:46 PM
Though 'p' is declared in the Main() method why program is producing error message.

Error1
Use of unassigned local variable 'p'



using System;

namespace _6e9
{
class Program
{
static void Main(string[] args)
{
double sum = 0, p;
int x;

double[] price = new double[5];

for (x = 0; x < price.Length; ++x)
{
Console.Write("Price of item #{0} $", x + 1);
p = Convert.ToDouble(Console.ReadLine());
sum = sum + p;
}

for (x = 0; x < price.Length; ++x)
{
if (p < 5)
{
Console.WriteLine("${0} is less than $5", p);
Console.WriteLine();
}
}
Console.ReadKey();
}
}
}


Answers (3)