背诵和提示输出对齐C++,输出格式 [英] recite and tip output alignment C++, output formatting

查看:58
本文介绍了背诵和提示输出对齐C++,输出格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的编程课编写这段代码,其他一切都可以正常工作,但是我的输出格式不适合我.

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.

我的输出看起来像这样

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.

我在使用带有浮点数的 setw 时遇到问题,但是当我尝试将相同的变量设置为 int 时它不起作用.我也试过使用 setw(25) 来看看它是否会以某种方式起作用,不幸的是它没有

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

推荐答案

需要使用 right 如果想让它们向右对齐,还需要在 <后面加一个空格""代码>美元

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

这篇关于背诵和提示输出对齐C++,输出格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆