0

このプログラムは基本的に、ファイルからデータを読み取り、そのデータに応じてそのデータを処理することになっています。模擬ケータリング会社のようなもので、変数は大人の人数、子供の人数、食事の種類 (デラックスまたはスタンダード)、曜日の種類 (週末 [はいまたはいいえ]、頭金など、追加料金、税金です。 、合計などは、そのデータに応じて CalcData 関数で計算されます (つまり、豪華な食事 (D または S) の場合、価格は 21.75 ドル (標準の場合) ではなく、週末の場合 ( YまたはN)、追加料金は合計請求額に加算され、合計額に応じて割引が適用されます)。

私は自分の関数で参照を使いすぎていると思いますが、プログラムはエラーチェック部分なしで正常に動作しました (つまり、入力が有効であったかどうかを確認します - 大人/子供/初期預金の金額にマイナスはなく、S/ 以外の文字はありません) D および/または Y/N など)。最初に bool を返す「isValid」関数と「outputErrorFile」関数を使用し、main で if/else を使用しました。データが無効な場合はエラー ファイルに出力し、無効でない場合はエラー ファイルに出力します。 、それを「Billing Statement」テキストファイルに出力するだけです。私はそれ以来、「checkValid」関数で2つを組み合わせました。同じことをするので、2 つの別々の機能を持つ必要はないと思います。

現在、すべてをエラー ファイルに出力しています (具体的には、bool 変数 "valid" はすべてのデータで常に false です)。私はそこで愚かなことをしていると確信しています。コンソールに何が出力されるかはあまり気にしません。テキスト ファイルに何が出力されるかだけが気になります...ご覧いただきありがとうございます。

ありがとう。

入力ファイル (大人、子供、デラックスまたはスタンダードミール、週末 (Y/N)、初回デポジット):

10 0 シリング 100.00

27 3DY 57.50

125 17DN 0.00

4 0 SN 25.00

0 25 SY 23.75

250 43 DN 500.00

0 0 DN 0.0

10 0 RY 10.00

17 3 DR 15.00

5 0 DY 275.00

-3 10 DY 20.00

14 -1 SN 30.00

20 3 日 -10.00

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

void getData(int &, int &, char &, char &, float &);
void checkValid(int &, int &, char &, char &, float &, bool &);
void calcData(int, int, char, char, float, float &, float &, float &, float &);
void sendData(int, int, char, char, float, float &, float &, float &, float &);

ifstream inFile;
ofstream outFile("Billing_Statement.txt");
ofstream error_Report("Error_Report.txt");

//Declare the tax rate and weekend surcharge as constants.

const float taxRate = 0.18;
const float weekendSurcharge = .07;

int main()
{

bool valid = true;
float mealCost;
float totalTax;
float totalSurcharge;
float discountAmount;
int numAdults;
int numChildren;
char mealType;
char dayType;
float depositAmount;

cout << "\nThis program will calculate data for a catering company " << endl;

outFile << " Adults " << "Children  " << "Meal " << " Weekend " << setw(9) << "Deposit "
<< setw(6) << "Tax" << setw(11) << "Surcharge" << setw(10) << "Discount" << setw(12) <<
"Meal Cost" << endl;
error_Report << " Adults " << "Children  " << "Meal " << " Weekend " << setw(9) <<  
"Deposit " << endl;

inFile.open("file.txt");

if (!inFile) {
cout << "nError: File could not be opened. ";
exit(1);
}

while (!inFile.eof()) {

getData(numAdults, numChildren, mealType, dayType, depositAmount);
checkValid(numAdults, numChildren, mealType, dayType, depositAmount, valid);

if (valid == true)
{
    calcData(numAdults, numChildren, mealType, dayType, depositAmount, totalTax,   
totalSurcharge, discountAmount, mealCost);
    sendData(numAdults, numChildren, mealType, dayType, depositAmount, mealCost,
totalTax, totalSurcharge, discountAmount);
}}

cout << "\nA copy of this has created for your convenience in the file, 
\"Billing_Statement.txt \"" << endl;

inFile.close();
outFile.close();
error_Report.close();

return 0;

}

void getData(int &numAdults, int &numChildren, char &mealType, char &dayType, float 
&depositAmount)
{
inFile >> numAdults >> numChildren >> mealType >> dayType >> depositAmount;
}

void checkValid(int &numAdults, int &numChildren, char &mealType, char &dayType, float
&depositAmount, bool & valid)
{

if (numAdults < 0 || numChildren < 0)
valid = false;
else if (mealType != 'D' || mealType != 'S')
valid = false;
else if (dayType != 'Y' || dayType != 'N')
valid = false;
else if (depositAmount < 0)
valid = false;

else
valid = true;

if (valid == false) {

error_Report << setw(7) << numAdults << setw(9) << numChildren << setw(6) << mealType <<
setw(9) << dayType << setw(9) << right << depositAmount << setw(8) << endl;
}
}

void calcData(int numAdults, int numChildren, char mealType, char dayType, float   
depositAmount, float &totalTax, float &totalSurcharge, float &discountAmount, float 
&mealCost)
{

if (mealType == 'S') {

mealCost = ((numAdults * 21.75) + (numChildren * (21.75 * .60)));
totalTax = mealCost * taxRate;
mealCost += taxRate;

if (dayType == 'Y') {
    totalSurcharge = mealCost * weekendSurcharge;
    mealCost += totalSurcharge;
}}

else {

mealCost = ((numAdults * 25.80) + (numChildren * (25.80 * .60)));
totalTax = mealCost * taxRate;
mealCost += taxRate;

if (dayType == 'Y') {
    totalSurcharge = mealCost * weekendSurcharge;
    mealCost += totalSurcharge;
    }
}

if (mealCost < 100) {

discountAmount = .015 * mealCost;
mealCost -= discountAmount;
}

else if (mealCost >= 100 && mealCost < 400) {

discountAmount = .025 * mealCost;
mealCost -= discountAmount;
}

else if (mealCost >= 400) {

discountAmount = .035 * mealCost;
mealCost -= discountAmount;
}
}

void sendData(int numAdults, int numChildren, char mealType, char dayType, float 
depositAmount, float &mealCost, float &totalTax, float &totalSurcharge, float 
&discountAmount)
{
outFile << fixed << showpoint << setprecision(2);
outFile << setw(7) << numAdults << setw(9) << numChildren << setw(6) << mealType << 
setw(9) << dayType << setw(9) << right << depositAmount << setw(8) << totalTax << 
setw(10) << totalSurcharge << setw(10) << right << discountAmount << setw(12) << right 
<< mealCost << endl;
}
4

1 に答える 1