0

ok so i got marked off on my program for hard coding, but not sure what i did wrong, here is a part of my program that was 'hardcoded'

cout << "Tax $" << (meal_price * guests) * TAX << endl;
cout << "Tip $" << (meal_price * guests + meal_price * guests * TAX) * TIP << endl;
cout << "Total $" << (meal_price * guests) + (meal_price * guests * TAX) + (meal_price * guests + meal_price * guests * TAX) * (TIP);

how is hardcoding defined in the statements above? how can it be non hardcoded? thanks

billz
  • 44,644
  • 9
  • 83
  • 100
  • I am guessing that `TIP` and `TAX` are constants whereas your instructor expected them to be variables. Also, you should store `(meal_price * guests)` in a variable as you are reusing the value. – asheeshr Feb 21 '13 at 03:46
  • 11
    Nothing wrong with this question in particular, but in my day, when we got marked down and didn't understand why, we talked to the instructor instead of to the internet. I'd imagine that this is still the most effective method today. – us2012 Feb 21 '13 at 03:50

1 Answers1

1

I don't see anything 'hardcoded' in that section, but if that is the section your instructor specifically pointed out, then they probably meant they wanted something like:

double total_price = meal_price * guests;
double total_tax = total_price * TAX;
double total_tip = total_price + total_tax * TIP;
double total = total_price  + total_tax  + total_tip;

std::cout << "Tax $" << total_tax  << std::endl;
std::cout << "Tip $" << total_tip  << std::endl;
std::cout << "Total $" << total  << std::endl;

Because your output lines are far too cluttered and most (if not all) calculations should be done outside of output lines.

You should defiantly go ask your instructor about this the first chance you get as they will know exactly what they meant and how to fix it.

I notice that TAX and TIP are all uppercase, does this mean you used #define to set them?
If so that may be where your instructor marked you down.

Serdalis
  • 10,296
  • 2
  • 38
  • 58