1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <string>
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;
string line;
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 (getline(inFile, line)){
istringstream ss(line);
while(ss >> numAdults >> numChildren >> mealType >> dayType >> depositAmount){
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;
}
|