私は学校のための「簡単な現金登録」プログラムを作成しようとしています。プログラムは、ユーザーに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で表示される実際の数額ためのプログラムに追加するべきです?
コメントのように特定のitemPurchasesの小計額を計算するコードを追加する必要があります。 subTotal = itemPurchases [5]は、範囲外にインデックスを作成しているため(indexingが0から始まるため)、間違っています。 あなたがするべきことは、あなたのクラスで注意を払い、あなたの宿題をすることです。 –
あなたのコードがうまくいかず、その理由がわからない場合は、*簡単なものを試してみてください*。 1つの値を受け入れて表示するコードを作成してください。上記のコードでは、値を変数のセットに読み込み、別の変数の内容を出力していることがわかります。 – Beta