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();
}
}
}
Unassigned local variable
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.

Roy S194 reputationPosted Dec 24, 2011, 12:14 PM EST
You know that the for loop will run 5 times but the compiler doesn't (giving you an error just in case). So just set p to some value (0 will do but doesn't matter) and it will work.
(...)
double sum = 0, p = 0;
(...)
VulpesRank #55 · 29.2K reputationPosted Dec 25, 2011, 12:24 AM EST
Posted Dec 25, 2011, 1:00 AM EST