Question
C++ PROGRAM QUESTION Module 10 Question 1A Complete the prog38.cpp to display random records from the random file . Question 1B Extend this to a
C++ PROGRAM QUESTION
Module 10
Question 1A
Complete the prog38.cpp to display random records from the random file.
Question 1B
Extend this to a menu driven program to create a random file, read it sequentially and read it randomly.
PROG38.cc
#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
************************************************************************
// Fig. 14.9: ClientData.h
// Class ClientData definition used in Fig. 14.11-Fig. 14.14.
#ifndef CLIENTDATA_H
#define CLIENTDATA_H
using namespace std;
#include
class ClientData
{
public:
// default ClientData constructor
ClientData( int = 0, const string & = "", const string & = "", double = 0.0 );
// accessor functions for accountNumber
void setAccountNumber( int );
int getAccountNumber() const;
// accessor functions for lastName
void setLastName( const std::string & );
std::string getLastName() const;
// accessor functions for firstName
void setFirstName( const std::string & );
std::string getFirstName() const;
// accessor functions for balance
void setBalance( double );
double getBalance() const;
private:
int accountNumber;
char lastName[ 15 ];
char firstName[ 10 ];
double balance;
}; // end class ClientData
#endif
/**************************************************************************
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************/
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