2017-01-23 71 views
2

我想爲學校創建一個「簡單的收銀機」程序。該計劃應詢問用戶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

回答

0

看起來您將項目的成本存儲在一個名爲itemPurchases的數組中,但是當您顯示每個項目的成本時,您將從程序開始時初始化爲0.0的變量顯示它們,並且從未更改。另外,當你計算小計時,你只是給它分配一個值(因爲數組的下標從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); 

使用新的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; 
} 

其實,你可以做你的代碼更多的改進。

0

subTotal = itemPurchases [5];

在這裏,您試圖訪問的第六屆指數5項指標陣列..

而且你可以將這個小計等於只有一個項目在數組中(不是所有項目的總和)

爲什麼不只在進入每次購買時遞增for循環內的小計?

相關問題