Reputation: 601
I've tried everything I can, it just isn't outputting the discounted information for a pensioner, when entered y, the cost is at the bottom of the program labeled pensionerdiscount. Any help would be appreciated, thank you.
const int MaxRooms = 5; // max size of array needed
double[] roomCosts = new double[MaxRooms];
string pensioner; // Pensioner discount ? y for yes, n for no
int nRooms;
double area, mCost, lCost; // area, materials and labour costs
int i;
string customer;
double totalCost = 0.0;
double totallabour = 0.0; // calculated seperately to allow discount
const double setupCost = 30.00; // setup cost/room
double pensionerDiscountRate = 0.1; // 10% discount for pensioner
double discount, discountedTotal;
double[] basePrices = {0, 4.0, 8.0, 12.0 };
int jobType; // used to index basePrices
// prices per sq metre for paint only, paint+prep, prep+paper+paint
Console.Write("enter name of customer: ");
customer = Console.ReadLine();
Console.Write("\nenter number of rooms to quote for: ");
nRooms = Convert.ToInt32(Console.ReadLine());
Console.Write("\nPensioner discount (y/n) ? ");
pensioner = Console.ReadLine().ToUpper();
Console.WriteLine();
for (i = 0; i < nRooms; i++)
{
Console.Write("enter area for room {0}: ", i + 1);
area = Convert.ToDouble(Console.ReadLine());
Console.Write("enter job type for room {0} " +
"(1 - paint only, 2 paint+prep, 3 paint,prep+paper ", i + 1);
jobType = Convert.ToInt32(Console.ReadLine());
int newVariable = jobType + 1;
lCost = area * basePrices[newVariable] + setupCost; // labour cost
totallabour += lCost; // total labour cost separately
Console.Write("\nenter materials cost for room {0}: ", i + 1);
mCost = Convert.ToDouble(Console.ReadLine());
Console.WriteLine();
roomCosts[i] = lCost + mCost; // store labour and material costs for room
totalCost += roomCosts[i];
}
// main report heading
Console.WriteLine("Decorators R US Job Quotation");
Console.WriteLine("=============================");
Console.WriteLine();
Console.WriteLine("\nName of customer: {0}\n", customer);
Console.WriteLine();
Console.WriteLine("No. of rooms: {0}", nRooms);
// output subheadings
Console.WriteLine("Room\tCost\t%of Total");
for (i = 0; i > nRooms; i++)
{
Console.Write("{0}\t", i + 1);
Console.Write("£{0:0.00}\t", roomCosts[i]);
Console.WriteLine("{0:00.0}%", roomCosts[i] * 100 / totalCost);
}
Console.WriteLine("Total Cost £{0:0.00}", totalCost);
if (pensioner == "y")
{
// deduct pensioner discount based on 10% of labour cost
discount = totallabour * pensionerDiscountRate;
discountedTotal = totalCost - discount;
Console.WriteLine("Pensioner Discount - £{0:0.00}", discount);
Console.WriteLine("Discounted Cost £{0:0.00}", discountedTotal);
Console.WriteLine();
}
Console.WriteLine("press enter to continue");
Console.ReadLine();
}
}
}
Upvotes: 1
Views: 97
Reputation: 3881
You're using ToUpper()
to get the discount (y/n):
Console.Write("\nPensioner discount (y/n) ? ");
pensioner = Console.ReadLine().ToUpper(); // USING ToUpper()
but later checking if (pensioner == "y")
which is lower case.
Use:
pensioner = Console.ReadLine().ToLower();
or keep it ToUpper()
and check with:
if (pensioner == "Y")
Upvotes: 3
Reputation: 1391
pensioner is being read in as an upper-case value, so it will never be "y". It could only ever be "Y", which doesn't match.
Temporarily adding this
Console.WriteLine("Pensioner: {0}", pensioner);
immediately before the comparison is very useful for identifying the exact value at the moment of comparison, too.
Upvotes: 2
Reputation: 112324
This is because you set pensioner to upper case
pensioner = Console.ReadLine().ToUpper();
and the test for lower case
if (pensioner == "y")
change it to
if (pensioner == "Y")
Upvotes: 2