gpt4 book ai didi

C++ cout分离

转载 作者:搜寻专家 更新时间:2023-10-31 02:09:43 26 4
gpt4 key购买 nike

我想知道是否有办法设置 w(#) 以便我的 couts 对齐,如果我要更改我的措辞,它们将继续对齐。所以我想我要问的是打印出与指标分开的值,几乎就像在自己的列中一样??

示例输出:(原创)

Enter the meal price for the first guest: $45.00
Enter the meal price for the second guest: $50.00
Enter the meal price for the third guest: $100.00

Tax: $13.16
Tip: $41.63
Total Bill: $249.79
Tip Per Person: $13.88
Total Per Person: $83.26
The 3rd guest saved: $16.74

(更改文本)

Enter the meal price for the first guest: $45.00
Enter the meal price for the second guest: $50.00
Enter the meal price for the third guest: $100.00

Tax: $13.16
Gratuity: $41.63
Total Bill: $249.79
Tip Per Person: $13.88
Total Per Person: $83.26
The 3rd guest saved: $16.74

(我的代码)

#include <iomanip> //std:setprecision
#include <iostream>

using namespace std;

int main() {

double ppl1, ppl2, ppl3, meal_total, tax, tip, total, total_bill, per_tip, per_total;

// Get the cost of the meal
cout << "Enter the meal price for the first guest: $";
cin >> ppl1;
// Get the cost of the meal
cout << "Enter the meal price for the second guest: $";
cin >> ppl2;
// Get the cost of the meal
cout << "Enter the meal price for the third guest: $";
cin >> ppl3;
cout << endl;
// Caluates the tax & tip & total
meal_total = ppl1 + ppl2 + ppl3;
tax = meal_total * 6.75 / 100;
tip = (tax + meal_total) * 20 / 100;
total_bill = (tax + meal_total) + tip;
per_tip = tip / 3;
per_total = total_bill / 3;

// I do not have the extra credit in place
cout << "\nTax: $" << setprecision(2) << fixed << tax;
cout << "\nTip: $" << setprecision(2) << fixed << tip;
cout << "\nTotal bill: $" << setprecision(2) << fixed << total_bill;
cout << "\nTip per person: $" << setprecision(2) << fixed << per_tip;
cout << "\nTotal per person: $" << setprecision(2) << fixed << per_total << endl;

// Additional Lab2A adding starts here:
// Adding vars to calulate savings on an per User basis
// then "If" the "Guest total" is greater then 0 we are displaying savings

double ppl1_Tax = ppl1 * 6.75 / 100;
;
double ppl1_Tip = (tax + ppl1) * 20 / 100;
double Price_ppl1 = (ppl1 + ppl1_Tax) + ppl1_Tip;
if ((Price_ppl1 - per_total) > 0) {
cout << "The 1st guest saved: $" << (Price_ppl1 - per_total) << endl;
}

double ppl2_Tax = ppl2 * 6.75 / 100;
;
double ppl2_Tip = (tax + ppl2) * 20 / 100;
double Price_ppl2 = (ppl2 + ppl2_Tax) + ppl2_Tip;
if ((Price_ppl2 - per_total) > 0) {
cout << "The 2nd guest saved: $" << (Price_ppl2 - per_total) << endl;
}

double ppl3_Tax = ppl3 * 6.75 / 100;
;
double ppl3_Tip = (tax + ppl3) * 20 / 100;
double Price_ppl3 = (ppl3 + ppl3_Tax) + ppl3_Tip;
if ((Price_ppl3 - per_total) > 0) {
cout << "The 3rd guest saved: $" << (Price_ppl3 - per_total) << endl;
}

return 0;
}

最佳答案

您回答了自己的问题,只需使用 setw(width),辅助函数示例:

void print_padded(const std::string& word, int width) {
std::cout << std::left << std::setw(width) << std::setfill(' ') << word;
};

添加更多信息,如果您不知道是否会有大于 widthwords,请跟踪最大的 word 并将 width 设置为 word.length() + 2 或类似的值。

关于C++ cout分离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46300487/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com