Question
Module 10 Question 1 a. Complete the prog38.cpp to display random records from random file. b. Extend this to a menu driven program to create
Module 10
Question 1
a. Complete the prog38.cpp to display random records from random file.
b. Extend this to a menu driven program to create a random file, read it sequentially and read it randomly.
This is prog c38.cpp:
#include
#include
#include
#include
#include "ClientData.h" // ClientData class definition
using namespace std;
void outputLine( ostream&, const ClientData & ); // prototype
int main()
{
ifstream inCredit( "credit.dat", ios::in | ios::binary );
// exit program if ifstream cannot open file
if ( !inCredit )
{
cerr << "File could not be opened." << endl;
exit( EXIT_FAILURE );
} // end if
// output column heads
ClientData client; // create record
int r;
cout << "Enter record to read ";
cin >> r;
inCredit.seekg (sizeof(ClientData)*(r-1), ios::cur);
// read first record from file
inCredit.read( reinterpret_cast< char * >( &client ),
sizeof( ClientData ) );
cout << left << setw( 10 ) << "Account" << setw( 16 )
<< "Last Name" << setw( 11 ) << "First Name" << left
<< setw( 10 ) << right << "Balance" << endl;
outputLine( cout, client );
// read next from file
#include
#include
#include
#include
#include "ClientData.h" // ClientData class definition
using namespace std;
void outputLine( ostream&, const ClientData & ); // prototype
int main()
{
ifstream inCredit( "credit.dat", ios::in | ios::binary );
// exit program if ifstream cannot open file
if ( !inCredit )
{
cerr << "File could not be opened." << endl;
exit( EXIT_FAILURE );
} // end if
// output column heads
ClientData client; // create record
int r;
cout << "Enter record to read ";
cin >> r;
//TODO : Ask three options as first record, last record or the record number
// Read the record based on the user value
inCredit.seekg (sizeof(ClientData)*(r-1), ios::cur);
// read first record from file
inCredit.read( reinterpret_cast< char * >( &client ),
sizeof( ClientData ) );
cout << left << setw( 10 ) << "Account" << setw( 16 )
<< "Last Name" << setw( 11 ) << "First Name" << left
<< setw( 10 ) << right << "Balance" << endl;
outputLine( cout, client );
}
void outputLine( ostream &output, const ClientData &record )
{
output << left << setw( 10 ) << record.getAccountNumber()
<< setw( 16 ) << record.getLastName()
<< setw( 11 ) << record.getFirstName()
<< setw( 10 ) << setprecision( 2 ) << right << fixed
<< showpoint << record.getBalance() << endl;
} // end function outputLine
// end while
} // end main
// display single record
void outputLine( ostream &output, const ClientData &record )
{
output << left << setw( 10 ) << record.getAccountNumber()
<< setw( 16 ) << record.getLastName()
<< setw( 11 ) << record.getFirstName()
<< setw( 10 ) << setprecision( 2 ) << right << fixed
<< showpoint << record.getBalance() << endl;
} // end function outputLine
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started