Exercise 8.3 asked you to write a series of single statements. Actually, these statements form the core of an important type of file-processing program, namely, a file-matching program. In com- mercial data processing, it is common to have several files in each application system. In an accounts receivable system, for example, there is generally a master file containing detailed information

about each customer, such as the customer’s name, address, telephone number, outstanding balance, cred- it limit, discount terms, contract arrangements and, possibly, a condensed history of recent purchas- es and cash payments. As transactions occur (e.g., sales are made and cash payments arrive), they are entered into a file. At the end of each business period (a month for some companies, a week for others and a day in some cases), the file of transactions (called trans.txt in Exercise 8.3) is applied to the master file (called oldmast.txt in Exercise 8.3), thus updating each account's record of purchases and payments. During an updating run, the master file is rewritten as a new file (newmast.txt), which is then used at the end of the next business period to begin the updating process again. File-matching programs must deal with certain problems that do not exist in single-file pro- grams. For example, a match does not always occur. A customer on the master file might not have made any purchases or cash payments in the current business period, and therefore no record for this customer will appear on the transaction file. Similarly, a customer who did make some pur- chases or cash payments may have just moved to this community, and the company may not have had a chance to create a master record for this customer. Use the statements from Exercise 8.3 as a basis for writing a complete file-matching accounts receivable program. Use the account number on each file as the record key for matching purposes. Assume that each file is a sequential file with records stored in increasing order by account number. When a match occurs (i.e., records with the same account number appear on both the master and transaction files), add the dollar amount on the transaction file to the current balance on the master file, and write the newmast.txt record. (Assume purchases are indicated by positive amounts on the transaction file and payments are indicated by negative amounts.) When there is a master record for a particular account but no corresponding transaction record, merely write the master record to newmast.txt. When there is a transaction record but no corresponding master record, print the error message "Unmatched transaction record for account number ..." (fill in the account number from the transaction record).

What will be an ideal response?


```
#include
#include
#include
#include
#include // exit function prototype
using namespace std;

void printOutput( ofstream&, int, string, string, double ); // prototype

int main()
{
int masterAccount; // holds account from old master file
int transactionAccount; // holds account from transactions file
double masterBalance; // holds balance from old master file
double transactionBalance; // holds balance from transactions file
string masterFirstName; // first name from master file
string masterLastName; // last name from master file

// file streams for input and output files
ifstream inOldMaster( "oldmast.txt" );
ifstream inTransaction( "trans.txt" );
ofstream outNewMaster( "newmast.txt" );

// terminate application if old master file cannot be opened
if ( !inOldMaster )
{
cerr << "Unable to open oldmast.txt\n";
exit( 1 );
} // end if

// terminate application if transactions file cannot be opened
if ( !inTransaction )
{
cerr << "Unable to open trans.txt\n";
exit( 1 );
} // end if

// terminate application if new master file cannot be opened
if ( !outNewMaster )
{
cerr << "Unable to open newmast.txt\n";
exit( 1 );
} // end if

// display account currently being processed
cout << "Processing...\n";
inTransaction >> transactionAccount >> transactionBalance;

// read from master file until end of transactions file reached
while ( !inTransaction.eof() )
{
inOldMaster >> masterAccount >> masterFirstName
>> masterLastName >> masterBalance;

// display accounts from master file until
// number of new account is reached
while ( masterAccount < transactionAccount && !inOldMaster.eof() )
{
printOutput( outNewMaster, masterAccount, masterFirstName,
masterLastName, masterBalance );
inOldMaster >> masterAccount >> masterFirstName
>> masterLastName >> masterBalance;
} // end while

// tell user if account from transactions file does not match
// account from master file
if ( masterAccount > transactionAccount )
{
cout << "Unmatched transaction record for account "
<< transactionAccount << '\n';

// get account and balance from transactions file
inTransaction >> transactionAccount >> transactionBalance;
} // end if

// if matching account found, update balance and output account info
if ( masterAccount == transactionAccount )
{
masterBalance += transactionBalance;
printOutput( outNewMaster, masterAccount, masterFirstName,
masterLastName, masterBalance );
} // end if

// get next account and balance from transactions file
inTransaction >> transactionAccount >> transactionBalance;
} // end while

// output remaining accounts to new master file
while ( !inOldMaster.eof() )
{
inOldMaster >> masterAccount >> masterFirstName
>> masterLastName >> masterBalance;
printOutput( outNewMaster, masterAccount, masterFirstName,
masterLastName, masterBalance );
} // end while
} // end main

// function to display output
void printOutput( ofstream &oRef, int mAccount, string mfName,
string mlName, double mBalance )
{
// set output format
cout << fixed << showpoint;
oRef << fixed << showpoint;

// display account number, name and balance
oRef << mAccount << ' ' << mfName << ' ' << mlName << ' '
<< setprecision( 2 ) << mBalance << '\n';
cout << mAccount << ' ' << mfName << ' ' << mlName << ' '
<< setprecision( 2 ) << mBalance << '\n';
} // end function printOutput
```
Processing...
100 Ajax Orange 678.06
300 Sue Green 650.07
400 Clint White 994.22
Unmatched transaction record for account 445
500 Aias Blue 895.80
700 Tom Black -23.57
900 Marisal Pink 200.55

Contents of oldmast.txt

100 Ajax Orange 543.89
300 Sue Green 22.88
400 Clint White -6.32
500 Aias Blue 888.71
700 Tom Black 76.09
900 Marisal Pink 100.55

Contents of trans.txt

100 134.17
300 627.19
400 1000.54
445 55.55
500 7.09
700 -99.66
900 100.00

Contents of newmast.txt

100 Ajax Orange 678.06
300 Sue Green 650.07
400 Clint White 994.22
500 Aias Blue 895.80
700 Tom Black -23.57
900 Marisal Pink 200.55

Computer Science & Information Technology

You might also like to view...

Critical Thinking QuestionsCase 3-1You know that your colleague Enrique does a masterful job maintaining his contacts in Outlook so when you are looking for advice on print styles, he is the person to whom you turn.You want to find a print style quickly that you know resembles Card style but is not Card style. Which of the following does Enrique tell you it could be? a. Medium Bookletc. Phone Directoryb. Memod. Business Card

What will be an ideal response?

Computer Science & Information Technology

Web services can include components such as currency conversion, weather reports, and language translation

Indicate whether the statement is true or false.

Computer Science & Information Technology

Unlike historical numbered ACLs (without sequence numbers), named ACLs allow a user to ________

A) Add or remove entries line by line B) Match hosts by name rather than IP address C) Use wildcards D) Add names for the common ports instead of numbers

Computer Science & Information Technology

____________________ link to folders containing the items you use the most, including the desktop, downloads, and recently opened files and folders.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology