What I need to do i something like this:
ClientData.h
#ifndef CLIENTDATA_H
#define CLIENTDATA_H
#include <string>
using std::string;
class ClientData
{
public:
ClientData(int = 0, string = "", string = "", double = 0.0);
void SetAccountNumber(int);
int GetAccountNumber() const;
void SetLastName(string);
string GetLastName() const;
void SetFirstName (string);
string GetFirstName() const;
void SetBalance (double);
double GetBalance() const;
private:
int _accountNumber;
char _lastName[15];
char _firstName[10];
double _balance;
};
#endif
ClientData.cpp
#include <string>
using std::string;
#include "ClientData.h"
ClientData::ClientData(int accountNumberValue, string lastNameValue, string firstNameValue, double balanceValue)
{
SetAccountNumber(accountNumberValue);
SetLastName(lastNameValue);
SetFirstName(firstNameValue);
SetBalance(balanceValue);
}
int ClientData::GetAccountNumber() const
{
return _accountNumber;
}
void ClientData::SetAccountNumber(int accountNumberValue)
{
_accountNumber = accountNumberValue;
}
string ClientData::GetLastName() const
{
return _lastName;
}
void ClientData::SetLastName(string lastNameString)
{
// copy at most 15 characters from string to lastName
const char* lastNameValue = lastNameString.data();
int length = lastNameString.size();
length = (length < 15 ? length : 14);
strncpy_s(_lastName, lastNameValue, length);
_lastName[length] = '\0';
}
string ClientData::GetFirstName() const
{
return _firstName;
}
void ClientData::SetFirstName(string firstNameString)
{
const char* firstNameValue = firstNameString.data();
int length = firstNameString.size();
length = (length < 10 ? length : 9);
strncpy_s(_firstName, firstNameValue, length);
_firstName[length] = '\0'; // append null character to firstName
}
double ClientData::GetBalance() const
{
return _balance;
}
void ClientData::SetBalance(double balanceValue)
{
_balance = balanceValue;
}
main.cpp
#include <iostream>
using std::cout;
using std::cin;
using std::cerr;
using std::ios;
using std::endl;
using std::left;
using std::right;
using std::showpoint;
using std::fixed;
#include <fstream>
using std::fstream;
using std::ostream;
using std::ifstream;
#include <iomanip>
using std::setw;
using std::setprecision;
#include "ClientData.h"
void OutputLine(ostream&, const ClientData& );
int main ()
{
//-----------------------------------------------------------
// Reading from a Random-Access File Sequentially
//-----------------------------------------------------------
ifstream inCredit("credit.dat", ios::in);
if (!inCredit)
{
cerr << "File could not be opened." << endl;
exit(1);
}
cout << left << setw(10) << "Account" << setw(16) << "Last Name" << setw(11) << "First Name" << left << setw(10) << right << "Balance" << endl;
ClientData client;
inCredit.read(reinterpret_cast<char*> (&client), sizeof(ClientData));
while (inCredit && !inCredit.eof())
{
//display record
if (client.GetAccountNumber() != 0)
OutputLine(cout, client);
//read next from file
inCredit.read(reinterpret_cast<char*> (&client), sizeof(ClientData));
}
cin.ignore();
cin.get();
return 0;
}
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;
}
This program reads records from a "credits.dat" file and prints all the records..
But
What I need to do is search for records and print 1 at a time.
for example i should enter the account number of a record and it should print all the information about that record. Keeping in mind that the archive "credits.dat" has many records...
I've been struggling trying to figure out a way to do this.
Any help would be much appreciated....