0

I'm writing this code for my programming class and I got everything else to work however my output formatting isn't working out for me.

#include <iostream>
#include <iomanip>
#include <ios>

    using namespace std;

    int main()
    {
        double tip_fifteen;
        double tip_twenty;
        double tax;
        double bill;
        char dollars = '$';
        float meal_cost;

        cout << "Enter the meal cost: ";
        cin >> meal_cost;
        tip_twenty = meal_cost * .20;
        tip_fifteen = meal_cost * .15;
        tax = meal_cost * 0.0975;
        cout << "******************************************" << endl;

        //beginning outputs
        int digits;
        digits = meal_cost * 100 / 100;
        cout << setw(10) << left << "Meal Cost " << dollars;
        cout << setw(to_string(digits).length() + 3) << fixed << showpoint << setprecision(2) << meal_cost << endl;

        cout << setw(10) << left << "Tax " << dollars;
        cout << setw(to_string(digits).length() + 3) << fixed << showpoint << setprecision(2) << tax << endl;

        cout << setw(10) << left << "Tip (15%) " << dollars;
        cout << setw(to_string(digits).length() + 3) << fixed << showpoint << setprecision(2) << tip_fifteen << endl;

        //tip outputs then final output statements
        cout << setw(10) << left << fixed << setprecision(2) << "Tip (20%) " << dollars << tip_twenty << endl;
        bill = tip_fifteen + meal_cost + tax;
        cout << "Your total bill is " << fixed << setprecision(2) << dollars << bill << " after 15% gratuity." << endl << "or" << endl;
        bill = tip_twenty + meal_cost + tax;
        cout << "Your total bill is " << fixed << setprecision(2) << dollars << bill << " after 20% gratuity." << endl;



return 0;

I want my output to look like this

Enter the meal cost: 56
******************************************
Meal Cost $ 56.00
Tax       $  5.46
Tip (15%) $  8.40
Tip (20%) $ 11.20
Your total bill is 69.86 after 15% gratuity.
or
Your total bill is 72.66 after 20% gratuity.

my output looks like this

Enter the meal cost: 56
******************************************
Meal Cost $56.00
Tax       $5.46 
Tip (15%) $8.40 
Tip (20%) $11.20
Your total bill is $69.86 after 15% gratuity.
or
Your total bill is $72.66 after 20% gratuity.

I'm having a problem using setw with floats however it's not working when i try to set the same variable as an int. I've also tried using setw(25) to see if that would work somehow unfortunately it has not

0

2 Answers 2

1

You need to use right if you want them aligned to the right, you also need to add a space " " after the dollars

cout << setw(10) << left << "Meal Cost " << dollars << " ";
cout << setw(to_string(digits).length() + 3) << fixed << right << showpoint << setprecision(2) << meal_cost << endl;

To this to all of the printed statements and you will get:

******************************************
Meal Cost $ 56.00
Tax       $  5.46
Tip (15%) $  8.40
Tip (20%) $ 11.20
Sign up to request clarification or add additional context in comments.

3 Comments

Note to Asker: the code for Tip (20%) does not match the code for the other outputs, so it will not render the same. Solution: Make it the same. The easiest way to do this is to write a function that does the formatting and is used by all of the related outputs. This also makes the program a bit shorter because there is less duplicated code. Duplication is nesting grounds for bugs.
What's different about "tip(20%)" I don't see how it doesn't match
@Brett: Tip 15: cout << setw(10) << left << "Tip (15%) " << dollars << cout << setw(to_string(digits).length() + 3) << fixed << showpoint << setprecision(2) << tip_fifteen << endl;. Tip 20: cout << setw(10) << left << fixed << setprecision(2) << "Tip (20%) " << dollars << tip_twenty << endl; tip 20 much, much shorter than Tip 15. It leaves out some of the formatting.
0

For there to be padding in the price column, setw has to be set to a large enough width.

For example:

std::cout << "$" << std::setw(10) << std::fixed
          << std::setprecision(2) << 56.0f << std::endl;

Prints:

$     56.00

Your code sets the width to:

std::to_string(digits).length() + 3

Which is only 5 characters, just enough to fit "56.00". For additional padding on the left you should increase the setw width.

2 Comments

but i want to alignment of numbers after the $
@Brett note how the << left earlier in the command was removed. Ultimately you need both left and a right to undo the left.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.