我正在努力为学校创建一个“简单的收银机”计划。该程序应询问用户5个购买金额,对购买金额应用固定税率,然后显示子总金额。这是我的代码:
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
/*
*
*/
int main(int argc, char** argv)
{
// Declare and initialize necessary variables
// You need to use floats for these
const float TAXRATE = 0.07; // 7% tax rate
float item1 = 0.0, item2 = 0.0, item3 = 0.0, item4 = 0.0, item5 = 0.0;
float subTotal = 0.0, taxTotal = 0.0, totalDue = 0.0;
float itemPurchases[5];
// Take 5 items as input
// Get item amounts from user
for (int i =0; i < 5; i++)
{
cout << "Please enter a purchased item" <<endl;
cin >> itemPurchases[i];
}
// Calculate subTotal, taxTotal, and totalDue
subTotal = itemPurchases[5];
taxTotal = subTotal * TAXRATE;
totalDue = subTotal + taxTotal;
// Drop down two lines on the output and print receipt header
cout << "\n" << endl;
cout << "Here is your receipt\n" << endl;
// Output the items
cout << fixed << setprecision(2); // Make sure all numbers have 2 decimals
cout << setw(15) << "Item 1 costs: $" << setw(10) << right << item1 << endl;
cout << setw(15) << "Item 2 costs: $" << setw(10) << right << item2 << endl;
cout << setw(15) << "Item 3 costs: $" << setw(10) << right << item3 << endl;
cout << setw(15) << "Item 4 costs: $" << setw(10) << right << item4 << endl;
cout << setw(15) << "Item 5 costs: $" << setw(10) << right << item5 << endl;
// Output single separation line
cout << "----------------------------" << endl;
// Output subTotals
cout << setw(15) << right << "Subtotal: $" << setw(10) << right << subTotal << endl;
cout << setw(15) << right << "Tax Total: $" << setw(10) << right << taxTotal << endl;
// Output double separation line
cout << "==========================" << endl;
cout << setw(15) << right << "Total Due: $" << setw(10) << right << totalDue << endl;
// End of program
return 0;
}
当我运行程序时,这就是我得到的:
Please enter a purchased item
5.00
Please enter a purchased item
6.00
Please enter a purchased item
7.00
Please enter a purchased item
8.00
Please enter a purchased item
9.00
Here is your receipt
Item 1 costs: $ 0.00
Item 2 costs: $ 0.00
Item 3 costs: $ 0.00
Item 4 costs: $ 0.00
Item 5 costs: $ 0.00
----------------------------
Subtotal: $ 0.00
Tax Total: $ 0.00
==========================
Total Due: $ 0.00
我的问题是我应该在程序中添加什么来显示实际的数量而不是0.00?
答案 0 :(得分:1)
尝试编写准确的代码
将ITEMAMOUNT添加为const值:
constexpr float ITEMAMOUNT = 5; // Item Amount (5)
删除此变量:
float item1 = 0.0, item2 = 0.0, item3 = 0.0, item4 = 0.0, item5 = 0.0;
使用浮动矢量
vector<float> itemPurchases(ITEMAMOUNT);
使用new for style
for (auto& item : itemPurchases)
{
cout << "Please enter a purchased item" << endl;
cin >> item;
subTotal += item;
}
使用for来打印项目
for (int i = 0; i < itemPurchases.size(); i++)
{
cout << setw(15) << "Item "<<i<<" costs: $" << setw(10) << right << itemPurchases[i] << endl;
}
实际上,您可以对代码进行更多改进。
答案 1 :(得分:0)
看起来您正在将项目的成本存储在名为itemPurchases的数组中,但是当您显示每个项目的成本时,您将从程序开始时初始化为0.0的变量中显示它们,并且从不更改。此外,当您计算小计时,您只需为其分配一个值(由于数组的下标从0开始,因此超出界限)。您可能希望通过添加数组的所有元素来获取小计。我希望这会帮助你。
答案 2 :(得分:0)
subTotal = itemPurchases [5];
此处您正尝试访问5索引数组中的第6个索引。
此外,您正在使subTotal仅等于数组中的一个项目(而不是所有项目的总和)
为什么不在每次购买时增加for循环内的小计?