Question
C++ PROGRAM QUESTION- ANSWER ONLY QUESTION 1B PLZ Module 10 Question 1A Complete the prog38.cpp to display random records from the random file . Question
C++ PROGRAM QUESTION- ANSWER ONLY QUESTION 1B PLZ
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
************************************************************************
ClientData.h
// 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. *
**************************************************************************/
************************************************************************
ClientData.cpp
// Fig. 14.10: ClientData.cpp
// Class ClientData stores customer's credit information.
#include
#include "ClientData.h"
using namespace std;
// default ClientData constructor
ClientData::ClientData( int accountNumberValue, const string &lastName,
const string &firstName, double balanceValue )
: accountNumber( accountNumberValue ), balance( balanceValue )
{
setLastName( lastName );
setFirstName( firstName );
} // end ClientData constructor
// get account-number value
int ClientData::getAccountNumber() const
{
return accountNumber;
} // end function getAccountNumber
// set account-number value
void ClientData::setAccountNumber( int accountNumberValue )
{
accountNumber = accountNumberValue; // should validate
} // end function setAccountNumber
// get last-name value
string ClientData::getLastName() const
{
return lastName;
} // end function getLastName
// set last-name value
void ClientData::setLastName( const string &lastNameString )
{
// copy at most 15 characters from string to lastName
int length = lastNameString.size();
length = ( length < 15 ? length : 14 );
lastNameString.copy( lastName, length );
lastName[ length ] = '\0'; // append null character to lastName
} // end function setLastName
// get first-name value
string ClientData::getFirstName() const
{
return firstName;
} // end function getFirstName
// set first-name value
void ClientData::setFirstName( const string &firstNameString )
{
// copy at most 10 characters from string to firstName
int length = firstNameString.size();
length = ( length < 10 ? length : 9 );
firstNameString.copy( firstName, length );
firstName[ length ] = '\0'; // append null character to firstName
} // end function setFirstName
// get balance value
double ClientData::getBalance() const
{
return balance;
} // end function getBalance
// set balance value
void ClientData::setBalance( double balanceValue )
{
balance = balanceValue;
} // end function setBalance
/**************************************************************************
* (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