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

标签 c++ fstream error-checking

这个程序基本上应该从文件中读取数据,然后根据数据的内容来处理该数据。这是一家模拟餐饮公司,变量是成人人数、 child 人数、用餐类型(豪华或标准)、一天的类型(周末[是或否]、初始押金等,以及附加费、税、总数等在 CalcData 函数中计算,具体取决于数据是什么(即,如果是豪华餐(D 或 S),价格为 25.80 美元,而不是 21.75 美元(对于标准),如果是周末 ( Y 或 N),附加费加到总账单上,并根据总金额给予折扣)。

虽然我认为我在我的函数中过度使用了引用,但该程序在没有错误检查部分的情况下运行良好(即检查输入是否有效 - 成人/ child /初始存款数量没有负数,没有其他字母比 S/D 和/或 Y/N 等)。我最初使用了一个返回 bool 的“isValid”函数和一个“outputErrorFile”函数,并在 main 中使用 if/else - 如果数据无效,则输出到错误文件,如果它不是无效的,然后将其输出到“帐单”文本文件。从那以后,我将两者结合在一个“checkValid”函数中。我认为做同样的事情,所以没有必要有两个单独的功能。

现在它正在将所有内容输出到错误文件(具体来说,bool 变量“valid”在我的所有数据中始终为 false)。我确定我在那里做了一些愚蠢的事情。我真的不关心输出到控制台的内容,只关心输出到文本文件的内容...感谢您的关注。

谢谢。

输入文件(成人、 child 、豪华或标准餐、周末(是/否)、初始押金):

10 0 S Y 100.00

27 3 D Y 57.50

125 17 DN 0.00

4 0 S N 25.00

0 25 S Y 23.75

250 43 DN 500.00

0 0 D N 0.0

10 0 是 10.00

17 3 D R 15.00

5 0 日 275.00

-3 10 日 20.00

14 -1 S N 30.00

20 3 D Y -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')

或者用 bool 逻辑重写

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

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

关于C++:模拟餐饮公司计费程序 - 无法将错误数据输出到错误文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20306541/

相关文章:

c++ - 从 txt 文件读取时,我的程序不会移动到下一行

c++ - C++ 中 std::fstream::X 和 std::ios::X 的区别

c++ - 使用 setprecision 在 C++ 中舍入小数字段时出现问题

c++ - 最好使用 getline 或 cin 从输入文件填充数组?

c++ - 在 C++ 中读取具有多个定界符的文件

C++ 通过错误检查使 sscanf 代码适应 istringstream

error-checking - 适当的错误检查量是多少?

Go errors : Is() and As() claim to be recursive, 是否有实现错误接口(interface)并支持此递归的任何类型 - 无错误?

c++ - 链表(删除节点)

c++ - 在 C++ 中读取具有相同文本的文本文件中的不同输出