1

I am very much a beginner programmer, finishing up Chapter 2 in Starting out with C++ (e7) by Tony Gladdis. I am doing my best to teach myself. Currently I am working on the Challenges at the end of the chapter. The problem I am having is how my output is displaying. With my current code, my output displays my numbers as 54.6293, when I just want it to display 54.62. Can someone please tell me how I can specify in my code to create my desired output?

/* Write a program that computes the tax and tip on a restraunt bill for a patron with a $44.50 meal charge. The tax should be 6.75 percent of the meal cost. The tip should be 15 percent of the total after adding the tax. Display the meal cost, tax amount, tip amount, and total bill on the screen*/

 #include <iostream>
    using namespace std;

    int main()
    {
        double Meal_Cost = 44.50,
            Total_After_Tax, Tax_Amount, Total_Bill, Total_Tip;
        const double TIP = 0.15,
            MEAL_TAX = 0.0675;

        // Calculate Tax_Amount
        Tax_Amount = Meal_Cost * MEAL_TAX;

        // Calculate Total_After_Tax
        Total_After_Tax = Tax_Amount + Meal_Cost;

        // Calculate Total_Tip
        Total_Tip = Total_After_Tax * TIP;

        // Calculate Total_Bill
        Total_Bill = Total_After_Tax + Total_Tip;

        // Display results
        cout << "The meal cost " << Meal_Cost << " dollars." << endl;
        cout << "The tax applied to the meal purchase was " << Tax_Amount << " dollars." << endl;
        cout << "The tip amount for the purchase was " << Total_Tip << " dollars." << endl;
        cout << "The total bill came to " << Total_Bill << " dollars." << endl;

        system ("PAUSE");
        return 0;
    }
Anna
  • 13
  • 3

1 Answers1

5

Yes, you can do:

#include <iomanip>

...

cout << "The total bill came to " << std::setprecision(2) << std::fixed 
     << Total_Bill << " dollars." << endl;

Note: When not using std::fixed, precision refers to the total amount of digits displayed. For example, 55. When using std::fixed, precision refers to the amount of digits displayed after the decimal point. For example, 54.63.

See this question for more details.

Community
  • 1
  • 1