C# Programming Error Help?

For some reason or another discount in the last method is an “unassigned local variable”. Can anyone explain why, would be most appreciated!
using System;
public class Discount
public static void Main()
{
int quantity;
double price;
quantity = GetQuantity();
price = CalculatePrice(quantity);
Console.WriteLine(“Final price for {0} items is {1}.”,
quantity, price.ToString(“C”));
}
public static int GetQuantity()
{
int quan;
Console.Write(“Enter number of items >> “);
quan = Convert.ToInt32(Console.ReadLine());
return quan;
}
public static double CalculatePrice(int quantityOrdered)
{
double PRICE_PER_ITEM = 6.00;
double price;
double discount;
int[] quanLimits = { 0, 5, 10, 15 };
double[] limits = { 0, 0.10, 0.14, 0.20 };
for (int x = limits.Length – 1; x >= 0; –x)
if (quantityOrdered >= limits[x])
{
discount = limits[x];
}
price = quantityOrdered * PRICE_PER_ITEM;
price = (price – price) * discount;
return price;
}
}

Both comments and pings are currently closed.

2 Responses to “C# Programming Error Help?”

  1. Riju says:

    you missed out { after the public class Discount

  2. peteams says:

    You’re calculate price code looks somewhat wrong.
    The error message is saying that there are paths through your code in which discount is not assigned a value. That is true, consider the simple case where limits is an empty array.
    Even if discount is assign a value, the penultimate line:
    price = (price – price) * discount;
    that is going to assign price to zero, (price – price) is going to be zero, so multiplying zero by anything will get you zero. I suspect that line should have been:
    price -= price * discount;
    or
    price *= (1 – discount);
    or
    price = price – price * discount;
    all of which should give the same result.

Powered by WordPress | Designed by: free css template | Thanks to hostgator coupon and web hosting reviews