Need help with incorrect outputs

Hello, I have the program that I am trying to write in the comments directly
below. My issue is when the program runs, I am getting answers completely incorrect. So I will get:
Meal Cost is $88.76 <--- This one is fine
Tax amount is $5.99 <---- This one is fine
Tip amount is $137.44
Total bill is $232.09

I thought it was something in my equations causing these problems but it does not seem to be so when I do the equations by hand. Any idea where I've gone wrong here would be great. Thank you.

// Restaurant Bill Write a program that computes the tax and tip on a restaurant bill for a patron with a
// $88.67 meal charge. The tax should be 6.75 percent of the meal cost. The tip should be 20 percent of the total
// after adding the tax. Display the meal cost, tax amount, tip amount, and total bill on the screen.

#include "pch.h"
#include <iostream>
#include <iomanip>
using namespace std;

double mealCharge = 0.8867;
double tax = 0.0675;
double tip = 0.20;
double taxAmount;
double tipAmount;
double totalBill;

int main()
{
taxAmount = (mealCharge * tax) * 100; // 0.5985
tipAmount = ((mealCharge + taxAmount) * tip) * 100; // should display 18.93
totalBill = (mealCharge * 100) + taxAmount + tipAmount;

cout << "The meal cost is $" << setprecision(4) << mealCharge * 100 << "." << endl;
cout << "The tax amount is $" << setprecision(3) << taxAmount << "." << endl;
cout << "The tip amount is $" << setprecision(5) << tipAmount << "." << endl;
cout << "The total bill is $" << setprecision(5) << totalBill << "." << endl;

return 0;
}
Why not just have your meal in its actual value instead of doing this * 100 thing?
i.e. double mealCharge = 88.67;
That would prevent you from ever having to do * 100 in your code.

* 100 is only needed for displaying percentages in ways that pesky humans like them (0.02 --> 20%).

This is what's causing your problem.

First:
Your tax percent is 6.75%
6.75% of $88.67 is $5.985225, which is 0.05985 in your program, not 0.5985. Your comment is off by a factor of 10.

Second:
When you do (mealCharge + taxAmount), your taxAmount is in real units ($5.9 bucks), but your mealCharge is in your "divided by 100" units.

Multiply mealCharge by 100, but not the overall result

tipAmount = ((mealCharge * 100 + taxAmount) * tip);

(or, put mealCharge in real units and forget all the * 100 stuff -- much simpler).
Last edited on
Topic archived. No new replies allowed.