我将如何着手在我的程序中创建计算?

我将如何着手在我的程序中创建计算?

问题描述:

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

+1

您应该添加代码,用于计算来自特定itemPurchases(如评论所述)的subTotal金额。 subTotal = itemPurchases [5]只是错误的,因为您正在索引超出范围(索引从0开始)。 你应该做的是注意你的课堂并做好功课。 –

+3

如果你的代码不起作用,你不知道为什么,*尝试更简单*。尝试编写接受一个值并显示它的代码;那么也许你会在上面的代码中看到,你正在将值读入一组变量,并打印另一组变量的内容。 – Beta

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

尝试写一个准确的代码

添加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); 

使用新的s tyle

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

使用的打印项目

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

其实,你可以做你的代码更多的改进。

subTotal = itemPurchases [5];

在这里,您试图访问的第六届指数5项指标阵列..

而且你可以将这个小计等于只有一个项目在数组中(不是所有项目的总和)

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