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;
You do not save the program, but you have to save the data that you take from the user. In Fig 14.12 - where is the lines in which you are actually writing data to the opened file? Because it is empty when you try to run 14.13.
Also, please edit your post. Use format icons to the right, namely the source code formatter <>.
The client data is entered using the Microsoft Windows Command Prompt after I select Start Without Debugging in Visual Studio for Fig14_12. The coding for Fig14_12 is written like it is in the book. If I am supposed to write the client data in the coding itself, where on Fig14_12 and/or Fig14_13 would I put it?
I do not know the exact instructions, but that would look like you are supposed to write to a file. Notice, that in 14_12 you are opening "credits.dat" as outCredit. Then you ask user to enter account number, first and last name and balance. Then the information is stored in memory in client struct. Then you seek the client in the file:
1 2 3 4 5 6 7 8 9
// seek position in file of user-specified record
outCredit.seekp((client.getAccountNumber() - 1) *
sizeof(ClientData));
// Why did you seek the position here? To write there, obviously.
// enable user to enter another account
cout << "Enter account number\n? ";
cin >> accountNumber;
And you do nothing about it. I believe this is the place in which the data in file should be updated.