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
// 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;
// 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;