Calculation Problems

I am writing a program for my beginning c++ class. It is supposed to create an invoice in the console window. so far, everything is working perfectly, except for one thing.

My invoice comes out looking like this:

696.60 - 27 Adult Meals at \$25.80 each.
+ 46.44 - 3 Child Meals at \$15.48 each.
--------
743.04 - Total Cost of All Meals (Add the above numbers together)
+ 52.01 - Weekend Surcharge (743.04 x .07)
+143.11 - Tax/Tip ((743.04 + 52.01) x .18)
--------
938.16 - Total Party Cost
--------
880.66 - Balance Due
- 32.84 - 3.5% Prompt Payment Discount (938.16 x .035)
--------
847.83 - Balance Due (If Paid Within Ten Days)

The problem is that if I add these numbers on a calculator, I get as result of \$847.82, not \$847.83. I checked them individually and they are all being rounded properly. If I add them up using the entire decimal value of the calculations, rather than the truncated value, I get \$847.83.

So, the question is as follows:

In C++, how do I do each calculation, round the answer to 2 decimal places, then use only the rounded number in the next calculation?
Last edited on
There may be other ways, but you could multiply by 100.0, add 0.5, then round down to the nearest integer. After that divide by 100.0.

 ``123`` `````` double num = 1234.5678; num = floor(num * 100.0 + 0.5) / 100.0;``````

result: 1234.57
I'm confused, what you want to do?
 If I add them up using the entire decimal value of the calculations, rather than the truncated value, I get \$847.83.
 In C++, how do I do each calculation, round the answer to 2 decimal places, then use only the rounded number in the next calculation?
 ``1234567891011121314151617181920212223242526272829303132333435`` ``````#include using std::cout; using std::endl; using std::fixed; #include using std::setprecision; int main(){ double adultMeals=27*25.80, childMeals=3*15.48, weekendSurcharge=(adultMeals+childMeals)*.07, taxTip=((adultMeals+childMeals)+weekendSurcharge)*.18, depositReceived=57.50, TotalPartyCost=((adultMeals+childMeals+weekendSurcharge+taxTip)), paymentDiscount=TotalPartyCost*.035, BalanceDue=TotalPartyCost-(depositReceived+paymentDiscount); cout<

 ``` Adult Meals: 696.600 Child Meals: 46.440 Weekend Surcharge: 52.013 Tax/Tip: 143.110 Deposit Received: -57.500 Total party cost: 938.162 Payment Discount: -32.836 Balance Due: 847.827 ```
I hear what you are saying. Let me give you the exact wording of the assignment and then links to my code. I would post the entire code here, but there are too many characters for this post to hold it.

http://www.jcitech.net/Images-ForumPosts/Lab6C-Dollars.txt is my old code (working in dollars and cents).

http://www.jcitech.net/Images-ForumPosts/Lab6C-Pennies.txt is the new code (working in pennies and converting everything to pennies).

The Output Data File is at http://www.jcitech.net/Images-ForumPosts/Lab6CDataFile-Output.txt

The Output Error file is at http://www.jcitech.net/Images-ForumPosts/Lab6CDataFile-Error.txt
----------------------------------
CSIT 575 – Programming Fundamentals for Computer Science Lab #6C

Objectives
To learn to code, compile and run a program containing three or more functions. This is a catering company.

Assignment
Plan and code a program utilizing one file for input and one file for output to solve the following problem:

(A) For adults, the deluxe meals will cost \$25.80 per person and the standard meals will cost \$21.75 per person, dessert included. Children's meals will cost 60 percent of adult meals. Everyone within a given party must be served the same meal type.

(B) A surcharge, currently 7 percent, is added to the total bill if the catering is to be done on the weekend (Friday, Saturday, or Sunday).

(C) All customers will be charged the same rate for tip and tax, currently 18 percent (applied only to the cost of the food).

(D) To induce customers to pay promptly, a discount is offered if payment is made within ten days.

(E) This discount depends on the amount of the total bill. If the bill is less than \$100.00, the discount is 1.5 percent; if the bill is at least \$100.00 but less than \$400.00, the discount is 2.5 percent; if the bill is \$400.00 or more, the discount is 3.5 percent.

Input
Number of adults, number of children, meal type (D, S) character indicating whether or not date is a weekend (Y/N), amount of any deposit. Create the data file below in text editor or Notepad.

Data File
http://www.jcitech.net/Images-ForumPosts/Lab6CDataFile-Input.txt

Output
Output an itemized bill listing the number of adults, children, cost for adult meals, cost for children's meals, total food cost, surcharge (if appropriate), tax and tip, total cost of the party, deposit (if any), total balance due, amount of discount if bill if paid within ten days. Output error data to a separate error file.

Turn in
Top down design with parameters, program listing and program output. Turn in all data files used in the program.

Question:
Since this is not a banking problem, I don't need that level of accuracy. Is there a way to look at the hundredth place on a decimal value and if it is > 4, then add 1 to the tenth place?
Last edited on
OK, after reading that article (thank you Paul) and many others, I added modified every calculation I was using that involved a decimal value (.18 for tax, .07 for surcharge and .015-.035 fro discount) to the following tResults.

 ``123`` `````` double tResult = 0; tResult = totalFCost * TIP_TAX_RATE; // .18 return floor((tResult + 0.005) * 100) / 100;``````
Ohh i get it now,that's great! :)