file processing program

what is this line doing?

Page 1 of 1

5 Replies - 1066 Views - Last Post: 25 July 2009 - 11:22 AM Rate Topic: -----

#1 angelita_azul  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 13-June 09

file processing program

Post icon  Posted 23 July 2009 - 09:04 PM

what is this line " inTransaction >> transactionAccount >> transactionBalance;" (in its first instance)
doing in this program? and also what is the function of the function? ;p (what is it doing in this program?)

#include <iostream> 
using std::cerr;
using std::cout; 
using std::fixed;
using std::showpoint;

#include <iomanip> 
using std::setprecision; 

#include <fstream>
using std::ofstream;
using std::ifstream;

#include <string>
using std::string;

#include <cstdlib> // exit function prototype
using std::exit;

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
   char masterFirstName[ 20 ]; // first name from master file
   char masterLastName[ 20 ]; // last name from master file

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

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

   // terminate application if new master file cannot be opened
   if ( !outNewMaster ) 
   {
	  cerr << "Unable to open newmast.dat\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

   inTransaction.close(); // close transactions file
   outNewMaster.close(); // close new master file
   inOldMaster.close(); // close old master file
   return 0;   
} // 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

This post has been edited by angelita_azul: 23 July 2009 - 09:07 PM


Is This A Good Question/Topic? 0
  • +

Replies To: file processing program

#2 cdk  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 32
  • Joined: 17-January 07

Re: file processing program

Posted 23 July 2009 - 09:28 PM

You can load this program into a debugger and step through the code to see how transactionAccount and transactionBalance are being changed.

You can replace the line inTransaction >> transactionAccount >> transactionBalance; with:
inTransaction >> transactionAccount;
inTransaction >> transactionBalance;


to see the same thing one change at a time.

The debugger will also let you see what the function is doing.
-cdk
Was This Post Helpful? 0
  • +
  • -

#3 angelita_azul  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 13-June 09

Re: file processing program

Posted 25 July 2009 - 06:07 AM

um can anyone else help?
Was This Post Helpful? 0
  • +
  • -

#4 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: file processing program

Posted 25 July 2009 - 07:07 AM

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



The first instance is loading the top record of "trans.dat", into the variables transactionAccount & transactionBalance.
Was This Post Helpful? 0
  • +
  • -

#5 angelita_azul  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 13-June 09

Re: file processing program

Posted 25 July 2009 - 09:51 AM

why is there a less than sign in this line. should the account number be equal?

while ( masterAccount < transactionAccount && !inOldMaster.eof())
Was This Post Helpful? 0
  • +
  • -

#6 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: file processing program

Posted 25 July 2009 - 11:22 AM

     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 



Why?

By the looks of it, if you match it up with the if after it, the data in "oldmast.dat" is sorted in ascending order based on transactionAccount. In the end if transactionAccound is not less/greater than masterAccount, it must be equal.

Not the most readable way to go about it, but it will work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1