Sending Data back to Main() #2

I am doing a homework assignment (I know there are a lot of those questions here). I am creating a program that takes input from a file and outputs various items in the form of a restaurant invoice to the screen, a file, as well as an error file. I wrote the whole thing and it works. However, now the teacher tells me that she wants Main() to control everything. Meaning, she wants each function to report back to Main() and Main() to call the next function.

I solved the issue that I posted in another thread, here is the new question.

Obviously, this is not ALL the code, as there is a character limit to posting here.

So here are the real questions:

1) The CalculateBill() function calls the PrintResults() function. How do I get it to send data back to Main() and allow Main() to call the PrintResults() function instead?

I could use structures, but that will require me to re-write the entire thing and then I will not understand my own code as structures are very new to me. Also, structures have not been taught in class yet and I do not want to go too far outside of what is being taught.

Is there a way to do this w/o using structures or a ton of global variables?

So here is what I have for now:

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
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

ifstream OpenInputfile( string inFilePath );
ofstream OpenOutputfile( string outFilePath, int fileType );
void PrintError(ostream & inFile);
bool ReadInputCheckErrors ( ifstream & inData );
void CalculateBill( int inAdults, int inChildren, bool inMealType, bool inWeekend, 
                        double inDeposit );
void PrintResults( ostream & inFile, int inAdults, int inChildren, bool inDeluxeMeal, 
                        bool inWeekend, double inDeposit, double ADULT_DELUXE, 
                        double ADULT_STANDARD, double childDeluxe, double 
                        childStandard, double tACost, double tCCost, double tTCost, 
                        double tSurcharge, double tTaxTip, double tPCost, double 
                        tBalanceDue, double tDiscount, string tDiscountPercent, 
                        double tDiscountBalance );
bool verboseError	= false;
bool saveDataFile	= false;
bool saveErrorFile	= true;
bool displayBill	= true;

ifstream inData;
ofstream outData;
ofstream errData;
bool deluxe		= 0;
bool weekend	= 0;
int numAdults	= 0;
int numChildren	= 0;
double deposit	= 0.00;


1
2
Because of lack of space, you can view the contents of Main ()
at http://www.cplusplus.com/forum/beginner/99279/ 


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
void CalculateBill( int inAdults, int inChildren, bool inDeluxeMeal, bool inWeekend, 
                        double inDeposit )
{
	if( displayBill )
	{
		const double ADULT_DELUXE		= 25.80;
		const double ADULT_STANDARD	= 21.75;
		const double TIP_TAX_RATE		= .18;
		const double WEEKEND_SURCHARGE	= .07;
		double childDeluxe			= ADULT_DELUXE * .6;
		double childStandard			= ADULT_DELUXE * .6;
		double tACost				= 0;
		double tCCost				= 0;
		double tTCost				= 0;
		double tSurcharge			= 0;
		double tTaxTip				= 0;
		double tPCost				= 0;
		double tBalanceDue			= 0;
		double tDiscount				= 0;
		double tDiscountBalance		= 0;
		string tDiscountPercent			= "";

		if( inDeluxeMeal )
		{ /* Total cost of all the deluxe meals. */
			tACost = ADULT_DELUXE * inAdults;
			tCCost = childDeluxe * inChildren;
		}
		else
		{ /* Total cost of all the standard meals. */
			tACost = ADULT_STANDARD * inAdults;
			tCCost = childStandard * inChildren;
		}

	// Calculations
		tTCost = tACost + tCCost; /* Total cost of the food. */
		if( inWeekend == true )
		{
			tSurcharge = tTCost * WEEKEND_SURCHARGE; /* Calculate the surcharge */
		}
		tTaxTip = (tTCost + tSurcharge) * TIP_TAX_RATE; /* Calculate the tax and tip. */
		tPCost = tTCost + tTaxTip + tSurcharge; /* Add up total cost, tax, tip and surcharge. */
	
	// Calculate the available discount.
		if( tPCost < 100.00 )
		{
			tDiscount = tPCost * .015;
			tDiscountPercent = "1.5%";
		}
		else if( tPCost < 400.00 )
		{
			tDiscount = tPCost * .025;
			tDiscountPercent = "2.5%";
		}
		else
		{
			tDiscount = tPCost * .035;
			tDiscountPercent = "3.5%";
		}

		tBalanceDue = tPCost - inDeposit; /* Calculate the pre-discount balance due. */
		tDiscountBalance = tBalanceDue - tDiscount; /* Calculate the post-discount balance due. */

	// Pass calculations to the PrintResults function.
		if( saveDataFile )
		{
			PrintResults( outData, inAdults, inChildren, inDeluxeMeal, 
                        inWeekend, inDeposit, ADULT_DELUXE, ADULT_STANDARD, 
                        childDeluxe, childStandard, tACost, tCCost, tTCost, tSurcharge, 
                        tTaxTip, tPCost, tBalanceDue, tDiscount, tDiscountPercent, 
                        tDiscountBalance );
		}
		if( displayBill)
		{
			PrintResults( cout, inAdults, inChildren, inDeluxeMeal, inWeekend, 
                        inDeposit, ADULT_DELUXE, ADULT_STANDARD, childDeluxe, 
                        childStandard, tACost, tCCost, tTCost, tSurcharge, tTaxTip, 
                        tPCost, tBalanceDue, tDiscount, tDiscountPercent, 
                        tDiscountBalance );
		}
	}
}


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
void PrintResults( ostream & inFile, int inAdults, int inChildren, bool inDeluxeMeal, 
                        bool inWeekend, double inDeposit, double ADULT_DELUXE, 
                        double ADULT_STANDARD, double childDeluxe, 
                        double childStandard, double tACost, double tCCost, 
                        double tTCost,  double tSurcharge, double tTaxTip, 
                        double tPCost, double tBalanceDue, double tDiscount, 
                        string tDiscountPercent, double tDiscountBalance )
{
		inFile << showpoint << fixed << setprecision (2);
		if( inDeluxeMeal )
		{
			if( inAdults > 0 )
			{
				inFile << "$" << setw(7) << tACost << "\t" << inAdults << " Adult Meals" 
					<< " at " << "$" << ADULT_DELUXE << " each" << endl;
			}
			if( inChildren > 0 )
			{
				inFile << "$" << setw(7)<< tCCost << "\t" << inChildren << " Child Meals" 
					<< " at " << "$" << childDeluxe << " each" << endl;
			}
		}
		else
		{
			if( inAdults > 0 )
			{
				inFile << "$" << setw(7)<< tACost << "\t" << inAdults << " Adult Meals" 
					<< " at " << "$" << ADULT_STANDARD << endl;
			}
			if( inChildren > 0 )
			{
				inFile << "$" << setw(7)<< tCCost << "\t" << inChildren << " Child Meals" 
					<< " at " << "$" << childStandard << endl;
			}
		}
		inFile << "--------" << endl;
		inFile << "$" << setw(7)<< tTCost << "\t" << "Total Cost of All Meals" << endl;
	
		if( inWeekend == true)
		{
			inFile << "$" << setw(7)<< tSurcharge << "\t" << "Weekend Surcharge" << endl;
			inFile << "--------" << endl;
			inFile << "$" << setw(7)<< tTCost + tSurcharge << "\t" << "Subtotal" << endl;
		}
		else
		{
		}

		inFile << "$" << setw(7)<< tTaxTip << "\t" << "Tax & Tip" << endl;
		inFile << "--------" << endl;
		inFile << "$" << setw(7)<< tPCost << "\t" << "Total Party Cost" << endl;
		if( inDeposit > 0)
		{
			inFile << "$" << setw(7)<< inDeposit << "\t" << "Deposit Received" << endl;
		}
		else
		{
			inFile << "$" << setw(7)<< "0.00" << "\t" << "Deposit Received" << endl;
		}
		inFile << "--------" << endl;

		if( tBalanceDue <= 0 )
		{
			double tRefundDue = inDeposit - ( tPCost - tDiscount );
			inFile << "$" << setw(7) << tDiscount << "\t" << tDiscountPercent 
				<< " Prompt Payment Discount" << endl;
			inFile << "--------" << endl;
			inFile << "$" << setw(7) << tRefundDue << "\t" << "Refund Due" << endl;
		}
		else
		{
			inFile << "$" << setw(7) << tBalanceDue << "\t" << "Balance Due" << endl;
			inFile << "$" << setw(7) << tDiscount << "\t" << tDiscountPercent 
				<< " Prompt Payment Discount" << endl;
			inFile << "--------" << endl;
			inFile << "$" << setw(7) << tDiscountBalance << "\t" 
				<< "Balance Due (If Paid Within Ten Days)" << endl;
		}

		inFile << endl;
		inFile << "- - - - - - - - - - - - - - - - - - - - - - -" << endl;
		inFile << endl;
}
Last edited on
After talking to my teacher, I found that the easiest way to accomplish this was to not use one function to calculate the invoice. Instead, use individual float functions that return pieces of the invoice. that way I can just send the variables to the print function.
Topic archived. No new replies allowed.