C++:模拟餐饮公司计费程序-无法将错误数据输出到错误文件



这个程序基本上应该从文件中读取数据,然后根据数据进行处理。它有点像一家模拟餐饮公司,变量包括成人人数、儿童人数、用餐类型(豪华或标准),一天的类型(周末[是或否]、首次存款等,以及附加费、税费、总额等在CalcData功能中根据数据进行计算(即,如果是豪华餐(D或S),价格为25.80美元,而不是21.75美元(标准餐);如果是周末(Y或N),则附加费将添加到总账单中,并根据总金额提供折扣)。

尽管我认为我在函数中过度使用了引用,但该程序在没有错误检查部分的情况下运行良好(即检查输入是否有效-成人/儿童/初始存款金额没有负数,除了S/D和/或Y/N之外没有其他字母,等等)。我最初使用了一个返回bool的"isValid"函数和一个"outputErrorFile"函数,并在main中使用if/else-如果数据无效,则输出到错误文件,如果不是无效,则仅输出到"账单声明"文本文件。此后,我将两者组合在一个"checkValid"函数中。做同样的事情,我认为,所以没有必要有两个单独的功能。

现在,它正在将所有内容输出到错误文件中(特别是,bool变量"valid"在我的所有数据中都是false)。我确信我在里面做了一些愚蠢的事。我并不真正关心输出到控制台的内容,只关心输出到文本文件的内容。。。谢谢你的光临。

谢谢。

输入文件(成人、儿童、豪华或标准餐、周末(是/否)、初始存款):

10 0 S Y 100.00

27 3天Y 57.50

125 17天N 0.00

4 0 S N 25.00

0 25 S Y 23.75

250 43天N 500.00

0 0 D N 0.0

10 0 R Y 10.00

17 3 D R 15.00

5 0天275.00

-3 10 D Y 20.00

14-1 S N 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;
}

这似乎是您对类型的检查,例如

mealType != 'D' || mealType != 'S'

将总是产生true,因此,valid总是被设置为false。你可能是指

!(mealType == 'D' || mealType == 'S')

或用布尔逻辑重写

mealType != 'D' && mealType != 'S'

顺便说一句,你的程序中也有其他错误。例如,我有一个最讨厌的问题:使用file.eof()控制输入循环总是错误的!您将对最后一行进行两次处理,或者,如果某个地方有一个格式错误的输入,则以无限循环结束。您总是需要在尝试读取后检查输入是否成功!流不可能提前知道你要读什么,以及这是否会成功。

最新更新