Could someone help me with this problem please?

I am working on a project covering pages 459-466 of C++11 for Programmers by Paul and Harvey Deitel, using Microsoft Visual Studio Express 2013. It involves files ClientData.h, ClientData.cpp, Fig14_12.cpp, and Fig14_13.cpp. After I run the program using the code in Fig14_12.cpp, I am supposed to enter the following information that is stored into a credit.dat file involving account numbers, last names, first names, and balances:

37 Barker Doug 0.00
29 Brown Nancy -24.54
96 Stone Sam 34.98
88 Smith Dave 258.34
33 Dunn Stacey 314.33

0 (states that I am done entering the data)

After I enter the data mentioned above, I am supposed to replace the code in Fig14_12 with that of Fig14_13 so that the data is listed in order by account number. However, when I run the program, the only row that is listed is Account, Last Name, First Name, and Balance, but none of the profiles are displayed.

My only guess is that I have to save the program that I run in Fig14_12 after entering the client data, before I enter the code from Fig14_13. If that is the case, How do I save a running program in Microsoft Visual Studio Express 2013? If not, what am I doing wrong?

Thanks

Here are the codes.

ClientData.h:

// Fig. 14.9: ClientData.h
// Class ClientData definition used in Fig. 14.11-Fig. 14.14.
#ifndef CLIENTDATA_H
#define CLIENTDATA_H

#include <string>

class ClientData
{
public:
// default ClientData constructor
ClientData(int = 0, const std::string & = "",
const std::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

ClientData.cpp:

// Fig. 14.10: ClientData.cpp
// Class ClientData stores customer's credit information.
#include <string>
#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

Fig14_12.cpp:

// Fig. 14.12: Fig14_12.cpp
// Writing to a random-access file.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "ClientData.h" // ClientData class definition
using namespace std;

int main()
{
int accountNumber;
string lastName;
string firstName;
double balance;

fstream outCredit("credit.dat", ios::in | ios::out | ios::binary);

// exit program if fstream cannot open file
if (!outCredit)
{
cerr << "File could not be opened." << endl;
exit(EXIT_FAILURE);
}// end if

cout << "Enter account number (1 to 100, 0 to end input\n? ";

// require user to specify account number
ClientData client;
cin >> accountNumber;

// user enters information, which is copied into file
while (accountNumber > 0 && accountNumber <= 100)
{
// user enters last name, first name, and balance
cout << "Enter lastname, firstname, balance\n? ";
cin >> lastName;
cin >> firstName;
cin >> balance;

// set record accountNumber, lastName, firstName, and balance values
client.setAccountNumber(accountNumber);
client.setLastName(lastName);
client.setFirstName(firstName);
client.setBalance(balance);

// seek position in file of user-specified record
outCredit.seekp((client.getAccountNumber() - 1) *
sizeof(ClientData));

// enable user to enter another account
cout << "Enter account number\n? ";
cin >> accountNumber;
}// end while
}// end main

Fig14_13.cpp:

// Fig. 14.13: Fig14_13.cpp
// Reading a random-access file sequentially.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#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
cout << left << setw(10) << "Account" << setw(16)
<< "Last Name" << setw(11) << "First Name" << left
<< setw(10) << right << "Balance" << endl;

ClientData client; // create record

// read first record from file
inCredit.read(reinterpret_cast<char *>(&client),
sizeof(ClientData));

// read all records from file
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));
}// 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
Last edited on
Topic archived. No new replies allowed.