What Am I Doing Wrong?

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
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?
Difficult to spot as you have not put your code in code tags

I can see you trying to read from a file, but looks like you need equivalent write methods to actually write to the file.

Take a look at this:
http://www.cplusplus.com/doc/tutorial/files/


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 imagine when you press '0' to state you've finished entering data that this would be a good time to write to the file.
Last edited on
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.
Topic archived. No new replies allowed.