我如何在程序中创建计算?

时间:2021-11-09 16:11:33

I am trying to create a "simple cash register" program for school. The program should ask the user for 5 purchase amounts, apply a constant tax rate to the purchase amounts and then display the sub total. Here is my code:

我正在努力为学校创建一个“简单的收银机”计划。该程序应询问用户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;
}

When I run the program this is what I get:

当我运行程序时,这就是我得到的:

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

My question is what should I add to the program for actual number amounts to be displayed instead of 0.00?

我的问题是我应该在节目中添加什么来显示实际的数量而不是0.00?

3 个解决方案

#1


1  

Try to write a accurate code

尝试编写准确的代码

Add ITEMAMOUNT as a const value:

将ITEMAMOUNT添加为const值:

constexpr float ITEMAMOUNT = 5; // Item Amount (5)

Remove this variables:

删除此变量:

float item1 = 0.0, item2 = 0.0, item3 = 0.0, item4 = 0.0, item5 = 0.0;

Use a vector of floats

使用浮点数向量

vector<float> itemPurchases(ITEMAMOUNT);

Use new for style

使用new作为样式

for (auto& item : itemPurchases)
{
    cout << "Please enter a purchased item" << endl;
    cin >> item;
    subTotal += item;
}

use a for to print items

使用a来打印项目

for (int i = 0; i < itemPurchases.size(); i++)
{
    cout << setw(15) << "Item "<<i<<" costs: $" << setw(10) << right << itemPurchases[i] << endl;
}

Actually, you can do more improvements on your code.

实际上,您可以对代码进行更多改进。

#2


0  

It appears that you are storing the cost of the items in an array called itemPurchases, however when you display the cost of each item, you are displaying them from variables initialized to 0.0 when the program began, and never changed. Also, when you calculate the subtotal, you are just assigning it a single value (which is out of bounds since an array's subscript begins at 0). You likely want to get the subtotal by adding all the elements of the array. I hope this helps you out.

您似乎将项目的成本存储在名为itemPurchases的数组中,但是当您显示每个项目的成本时,您将从程序开始时初始化为0.0的变量中显示它们,并且从不更改。此外,当您计算小计时,您只需为其分配一个值(由于数组的下标从0开始,因此超出界限)。您可能希望通过添加数组的所有元素来获取小计。我希望这能够帮到你。

#3


0  

subTotal = itemPurchases[5];

subTotal = itemPurchases [5];

Here You are trying to access the 6th index in a 5 index array..

这里您尝试访问5索引数组中的第6个索引。

Also you are making the subTotal equal to only one item in the array (not the sum of all items)

你也使subTotal只等于数组中的一个项目(而不是所有项目的总和)

Why not just increment the subtotal inside the for loop while entering each purchase?

为什么不在进入每次购买时增加for循环内的小计?

#1


1  

Try to write a accurate code

尝试编写准确的代码

Add ITEMAMOUNT as a const value:

将ITEMAMOUNT添加为const值:

constexpr float ITEMAMOUNT = 5; // Item Amount (5)

Remove this variables:

删除此变量:

float item1 = 0.0, item2 = 0.0, item3 = 0.0, item4 = 0.0, item5 = 0.0;

Use a vector of floats

使用浮点数向量

vector<float> itemPurchases(ITEMAMOUNT);

Use new for style

使用new作为样式

for (auto& item : itemPurchases)
{
    cout << "Please enter a purchased item" << endl;
    cin >> item;
    subTotal += item;
}

use a for to print items

使用a来打印项目

for (int i = 0; i < itemPurchases.size(); i++)
{
    cout << setw(15) << "Item "<<i<<" costs: $" << setw(10) << right << itemPurchases[i] << endl;
}

Actually, you can do more improvements on your code.

实际上,您可以对代码进行更多改进。

#2


0  

It appears that you are storing the cost of the items in an array called itemPurchases, however when you display the cost of each item, you are displaying them from variables initialized to 0.0 when the program began, and never changed. Also, when you calculate the subtotal, you are just assigning it a single value (which is out of bounds since an array's subscript begins at 0). You likely want to get the subtotal by adding all the elements of the array. I hope this helps you out.

您似乎将项目的成本存储在名为itemPurchases的数组中,但是当您显示每个项目的成本时,您将从程序开始时初始化为0.0的变量中显示它们,并且从不更改。此外,当您计算小计时,您只需为其分配一个值(由于数组的下标从0开始,因此超出界限)。您可能希望通过添加数组的所有元素来获取小计。我希望这能够帮到你。

#3


0  

subTotal = itemPurchases[5];

subTotal = itemPurchases [5];

Here You are trying to access the 6th index in a 5 index array..

这里您尝试访问5索引数组中的第6个索引。

Also you are making the subTotal equal to only one item in the array (not the sum of all items)

你也使subTotal只等于数组中的一个项目(而不是所有项目的总和)

Why not just increment the subtotal inside the for loop while entering each purchase?

为什么不在进入每次购买时增加for循环内的小计?