Sending Data back to Main()

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.

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
int main()
{
	inData	= OpenInputfile( "Lab6CDataFile-Input.txt" );
	outData	= OpenOutputfile( "Lab6CDataFile-Output.txt", 1 );
	errData	= OpenOutputfile( "Lab6CDataFile-Error.txt", 2 );

	while( !inData.eof() ) /* Process all lines of data. */
	{ 
		ReadInputLine(inData);
	}

	if( outData.good() )
	{
		if( verboseError )
		{
			cout << "Data file written successfully\n\n" << endl;
		}
		system("pause");
	}

	if ( saveErrorFile )
	{
             // I want the call to PrintError() to be here.
	}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ifstream openInputfile( string inFilePath )
{
	ifstream inStream;
	inStream.open( inFilePath, ios::in ); /* Open the inData File. */

	if( !inStream.is_open() ) /* Inputs file couldn't be opened. */
	{ 
		cout << "Error: file could not be opened" << "\n\n";
		system ("pause");
		exit(1);
	}
	if( verboseError ) {
              cout << "The input file ( " << inFilePath << " ) has been
              opened successfully!" << "\n\n"; }
	return inStream; }

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
void ReadInputLine( ifstream & inData )
{
	bool errOccurred = false;
	char tChar = 0;
	inData >> numAdults; /* Get the number of adults. */
	if( numAdults < 0 )
	{
		errOccurred = true;
	}

	inData >> numChildren; /* Get the number of children. */
	if( numChildren < 0 )
	{
		errOccurred = true;
	}

	if( numAdults == 0 && numChildren == 0 )
	{
		errOccurred = true;
	}

	inData >> tChar; /* Get Deluxe or Standard meal. */
	if( tChar == 'D' )
	{
		deluxe = true;
	}
	else if( tChar == 'S' )
	{
		deluxe = false;
	}
	else
	{
		errOccurred = true;
	}

	inData >> tChar; /* Get Weekend or Weekday. */
	if( tChar == 'Y' )
	{
		weekend = true;
	}
	else if( tChar == 'N' )
	{
		weekend = false;
	}
	else
	{
		errOccurred = true;
	}

	inData >> deposit; /* Get the amount of the deposit. */
	if( deposit < 0 )
	{
		errOccurred = true;
	}

	if( errOccurred == true )
	{
			PrintError (errData);

		if( verboseError )
		{
			cout << ".............................................\n\n";
			cout << "Error in Data:\t";
			PrintError (cout);
			cout << "\n.............................................\n\n";
		}
	}
	else
	{
		CalculateBill ( numAdults, numChildren, deluxe, weekend, deposit );
	}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void PrintError(ostream & inFile)
{
	inFile << numAdults	<<  "\t";
	inFile << numChildren	<<  "\t";

	if ( deluxe == true )
	{
		inFile << "D"	<<  "\t";
	}
	else
	{
		inFile << "S" << "\t";
	}
	if ( weekend == true )
	{
		inFile << "Y"	<<  "\t";
	}
	else
	{
		inFile << "N" << "\t";
	}
	inFile << deposit <<  "\n";
}


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

So here are the real questions:

1) The ReadInputLine function calls the PrintError function. How do I get it to send data back to Main() and allow Main() to call the PrintError 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.
Last edited on
I'm guessing you are using global variables.

What you can do is on failure have ReadInputLine return false.

1
2
if(!ReadInputLine(inData))
   PrintError(cout);

Sorry, the code above Main() is
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
#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);
void ReadInputLine ( 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;
Last edited on
OK, I tried that, but I found it easier to merge the two functions together. I am working on a new problem that cannot be merged, but will post a new thread about it.

Thanks.
Topic archived. No new replies allowed.