本文介绍了C ++:Mock餐饮公司帐单计划 - 不能输出错误的数据到错误文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 这个程序基本上应该从一个文件读取数据,然后根据它是什么处理该数据。它是一个模拟餐饮公司,变量是成年人数,孩子数,膳食类型(豪华或标准),类型(周末[是或否],初始存款等,以及附加费,税,总数等在CalcData函数中计算,取决于数据是什么(即如果它是一个豪华餐(D或S),价格是$ 25.80,而不是$ 21.75(标准),如果它是一个周末Y或N),附加费加到总帐单,并给出折扣,取决于总金额)。 虽然我认为我过度使用我的功能,程序工作正常,没有错误检查部分(即检查输入是/有效 - 成人/儿童/初始存款金额无负面,没有其他字母S / D和/或Y / N,等等)我最初使用一个isValid函数,它将返回一个bool和一个outputErrorFile函数,并在主体中使用if / else - 如果数据无效,然后输出到错误文件,如果它无效,然后只是将其输出到结算语句文本文件。我自从结合这两个在checkValid函数。同样的事情,我想,所以没有必要有两个单独的函数。 现在它输出的一切到错误文件(具体来说,bool变量, 有效,对我的所有数据不断为假)。我相信我在做一些蠢事。我真的不在乎什么是输出到控制台,只关心什么是输出到文本文件...谢谢你看。 谢谢。 INPUT FILE(成人,儿童,豪华或标准餐,周末(Y / N),初始按金): b $ b 10 0 SY 100.00 27 3 DY 57.50 125 17 DN 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 DY -10.00 #include< iostream> #include< fstream> #include< iomanip> using namespace std; void getData(int& amp;& amp; char& char& float; void checkValid(int& amp; int& char& amp; char& float; amp; bool& void calcData(int,int,char,char,float,float& float; amp; float& float; void sendData(int,int,char,char,float,float& float; amp; float& float; ifstream inFile; ofstream outFile(Billing_Statement.txt); ofstream error_Report(Error_Report.txt); //将税率和周末附加费作为常量声明。 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<< \\\该程序将计算餐饮公司的数据< endl; outFile<< 成人<< 儿童< Meal< 周末< setw(9)<< 存款<< setw(6)<< 税< setw(11)<< 附加费<< setw(10)<< 折扣<< setw(12)<< 膳食成本< endl; error_Report<< 成人<< 儿童< Meal< 周末< setw(9)<< 存款< endl; inFile.open(file.txt); if(!inFile){ cout<< nError:无法打开文件。 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<< \\\A的副本为您方便在文件中创建了 \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>>存款金额; } 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 valid = false; else valid = true; if(valid == false){ error_Report<< setw(7)<< numAdults<< setw(9)<< numChildren< setw(6)<< mealType<< setw(9)<< dayType<< setw(9)<<右< 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 = * 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 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<固定<显示点<< setprecision(2); outFile<< setw(7)<< numAdults<< setw(9)<< numChildren< setw(6)<< mealType<< setw(9)<< dayType<< setw(9)<<右< depositAmount<< setw(8)<< totalTax setw(10)<< totalSurcharge< setw(10)<<右< discountAmount<< setw(12)<< right << mealCost<< endl; } 解决方案 ,例如 mealType!='D'|| mealType!='S' 总会产生 true ,因此 valid 始终设置为 false 。您可能是指 !(mealType =='D'|| mealType =='S') 或用布尔逻辑重写 mealType!='D'&& mealType!='S' BTW,你的程序中还有其他问题。例如,有我的宠爱:使用 file.eof()来控制输入循环是总是错了!你将处理最后一行两次,或者,如果有一个错误的格式化输入,结束了一个无限循环。您始终需要在之后检查是否尝试读取输入是否成功!该流不可能提前知道你将要阅读什么,如果这将是成功的。 This program is basically supposed to read data from a file, and then process that data depending on what it is. It's sort of a mock catering company, and the variables are number of adults, number of children, type of meal (deluxe or standard), type of day (weekend [Yes or No], initial deposit, etc. and the surcharge, tax, total, etc. are calculated in the CalcData function depending on what that data is (i.e. If it is a deluxe meal (D or S), the price is $25.80, instead of $21.75 (for Standard), if it is a weekend (Y or N), the surcharge is added up to the total bill, and discounts are given, depending on the total amount).Although I think I am overusing references in my functions, the program has worked fine without the error checking part (i.e. check that the input was/is valid - No negatives for amount of adults/children/initial deposit, no other letters than S/D and/or Y/N, etc.). I initially used an "isValid" function that would return a bool, and an "outputErrorFile" function, and use an if/else in main - If the data was invalid, then output to the error file, and if it wasn't invalid, then just to output it to the "Billing Statement" text file. I since combined the two in a "checkValid" function. Does the same thing, I think, so there's no need to have two separate functions.Right now it is outputting everything to the error file (specifically, the bool variable, "valid," is constantly false on all of my data). I'm sure I'm doing something stupid in there. I don't really care about what is output to the console, only care about what is output to the text files...Thanks for looking.Thanks. INPUT FILE (adults, children, deluxe or standard meal, weekend (Y/N), Initial Deposit):10 0 S Y 100.0027 3 D Y 57.50125 17 D N 0.004 0 S N 25.000 25 S Y 23.75250 43 D N 500.000 0 D N 0.010 0 R Y 10.0017 3 D R 15.005 0 D Y 275.00-3 10 D Y 20.0014 -1 S N 30.0020 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;elsevalid = 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;} 解决方案 It seems your checks for types, e.g.mealType != 'D' || mealType != 'S'will always yield true and, thus, valid is always set to false. You probably meant!(mealType == 'D' || mealType == 'S')or rewritten with Boolean logicmealType != 'D' && mealType != 'S'BTW, there are other things wrong in your program, too. For example, there is on of my pet peeves: using file.eof() to control an input loop is always wrong! You will either process the last line twice or, if there is a misformatted input somehwere, end up with an infinite loop. You always need to check after trying to read if the input was successful! The stream cannot possibly know ahead of time what you will be trying to read and if that is going to be successful. 这篇关于C ++:Mock餐饮公司帐单计划 - 不能输出错误的数据到错误文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-30 09:02