Having some issues with my Error Output File

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 or 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.

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, 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 another 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. 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...Appreciate the help.

Thanks.

INPUT TEXT FILE:


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



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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#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 << endl;
 
    cout << " Adults " << "Children  " << "Meal " << " Weekend " << setw(9) << "Deposit " << setw(6) << "Tax" << setw(11) << "Surcharge" << setw(10) << "Discount" << setw(12) << "Meal Cost" << endl;
    outFile << " Adults " << "Children  " << "Meal " << " Weekend " << setw(9) << "Deposit " << setw(6) << "Tax" << setw(11) << "Surcharge" << setw(10) << "Discount" << setw(12) << "Meal Cost" << 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 convencience in file /" Billing_Statement.txt /".";
 
 
    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)
{
 
    cout << fixed << showpoint << setprecision(2);
 
    cout << 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;
 
    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;
 
}
Last edited on
Here are the instructions for the program:


A. For adults, the deluxe meals will cost $25.80 per person and the standard meals will cost $21.75 per person, dessert included. Children's meals will cost 60 percent of adult meals. Everyone within a given party must be served the same meal type.
B. All customers will be charged the same rate for tip and tax, currently 18 percent (applied only to the cost of the food).
C. A surcharge, currently 7 percent, is added to the total bill if the catering is to be done on the weekend (Friday, Saturday, or Sunday).
D. To induce customers to pay promptly, a discount is offered if payment is made within ten days.
E. This discount depends on the amount of the total bill. If the bill is less than $100.00, the discount is 1.5 percent; if the bill is at least $100.00 but less than $400.00, the discount is 2.5 percent; if the bill is $400.00 or more, the discount is 3.5 percent.
F. Output invalid input on a separate error file.
Last edited on
Just to update, I figured out what I was doing wrong. The following were always being evaluated to true, which made my program false.

1
2
3
(mealType != 'D' || mealType != 'S')

(dayType != 'Y' || dayType != 'N')


It will always evaluate to true since the input will either be one or the other every time.

(!(dayType == 'Y' || dayType == 'N')) is the correct use of Boolean logic here. Everything is fine with my program, except for the last line repeating itself.
Last edited on
Topic archived. No new replies allowed.