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.00
27 3 D Y 57.50
125 17 D N 0.00
4 0 S N 25.00
0 25 S Y 23.75
250 43 D N 500.00
0 0 D N 0.0
10 0 R Y 10.00
17 3 D R 15.00
5 0 D Y 275.00
-3 10 D Y 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;
}