您好,我在这里是不得已的方法。对于此作业,我无法弄清楚某些事情,例如如何将小数显示为整数。例如,.29应该是29.00。我也不知道如何正确显示项目数,就像有2个项目(3个鸡蛋和2个奶酪)一样,我怎么只显示2个项目而不是5个?
我已附上此作业的指导原则以及到目前为止的内容。谢谢您的帮助!
A部分 您的程序在启动时应从用户获得以下信息: •收银员的姓名。 •说明商店的所在地(亚利桑那州,纽约等) •日期(分为日,月和年)
B部分 该程序应以以下格式向用户显示欢迎消息:
您好(出纳名称)欢迎使用收银应用。 您目前正在为(州)的一家商店兑现。 今天的日期是(日期)。
该程序应允许用户输入不限数量的产品,产品名称,价格和购买数量。程序必须计算要添加到价格中的税额,具体取决于程序在以下三个州中的哪个州使用: 纽约-9.75% 新泽西州-8.25% 康涅狄格州-7.5% 田纳西州-4.5% 其他-10%
在计算了单个产品的欠款总额之后,程序应显示产品名称和总额,然后询问用户是否要输入其他产品,即
鸡蛋-$ 10.74
您想输入其他产品吗? 用户输入完所有产品后,您应该显示购买摘要,告诉用户输入了多少商品以及应付款总额。 您输入了14个产品。您的欠款总额为$ 845.89
#include <iostream>
using namespace std;
int main() {
string state, month, day, year, cashierName, productName;
char YorN;
float price, tax, productTotal, productQuantity, totalQuantity = 0, total = 0;
cout << "Enter name: ";
cin >> cashierName;
cout << "Are you in NY, NJ, CT, TN, or other?: ";
cin >> state;
cout << "Enter month: ";
cin >> month;
cout << "Enter day: ";
cin >> day;
cout << "Enter year: ";
cin >> year;
cout << "Hello " << cashierName << ". Welcome to Cashier App.\n";
cout << "You are currently cashing for a store located in " << state << ".\n";
cout << "Today's date is " << month << " " << day << ", " << year << ".\n";
if(state == "NY" || state == "ny") {
tax = .0975;
}
else if (state == "NJ" || state == "nj") {
tax = .0825;
}
else if (state == "CT" || state == "ct") {
tax = .075;
}
else if (state == "TN" || state == "tn") {
tax = .045;
}
else {
tax = .1;
}
cout << "Do you want to add a product to your cart? (Y/N) ";
cin >> YorN;
while(YorN == 'Y' || YorN == 'y') {
cout << "Enter product name: ";
cin >> productName;
cout << "Enter price: ";
cin >> price;
cout << "Enter quantity: ";
cin >> productQuantity;
productTotal = price * productQuantity * tax;
total = productTotal + total;
totalQuantity = productQuantity + totalQuantity;
int nProductTotal = int(productTotal * 100);
productTotal = ((float)nProductTotal)/100;
int nTotal = int(total * 100);
total = ((float)nTotal)/100;
cout << productName << " - $" << productTotal << endl;
cout << "Would you like to enter another another product? (Y/N) ";
cin >> YorN;
}
cout << "You have entered " << totalQuantity << " products. Your total amount owed is $" << total << ".\n";
return 0;
}
答案 0 :(得分:0)
要显示不同产品的数量,您可以用它们来计数
...
int totalQuantity(0);
while(YorN == 'Y' || YorN == 'y') {
++totalQuantity;
...
// remove totalQuantity = productQuantity + totalQuantity;
...
}
cout << "You have entered " << totalQuantity << " products. Your total amount owed is $" << total << ".\n";
...
totalQuantity
的类型应为int
,而不是float
,因为您不能购买2.2种不同的产品。可以将值四舍五入到小数点后两位
productTotal = std::round(price * productQuantity * tax * 100)/100;
请避免
这样的c样式强制转换(float)nTotal
执行此操作的c ++方法是
static_cast<float>(nTotal);
我不明白您对的意思。29应该是29.00 ,但是您可以通过
实现double a = 100 * .29;
或
int a = std::round(100 * .29);
或
cout << std::setprecision(2) << 0.29 * 100 << '\n';