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();
}
}
}
Loading
Posted Dec 25, 2011, 6:00 AM
VulpesPosted Dec 25, 2011, 5:24 AM
Roy SPosted Dec 24, 2011, 5:14 PM
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;
(...)