Help With Customer Account Project Code

Help with code. Has errors
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <iomanip>

using namespace std;

fstream custFile;
fstream tempFile;

const int NAME_LEN = 45;
const int ADDR_LEN = 45;
const int CITY_LEN = 20;
const int STATE_LEN = 4;
const int ZIP_LEN = 11;
const int PHONE_LEN = 14;
const int ARRAY_SIZE = 11;

// Structure used to represent customer data
struct Customer
{
char name [NAME_LEN];
char address [ADDR_LEN];
char city [CITY_LEN];
char state [STATE_LEN];
char zip [ZIP_LEN];
char phone [PHONE_LEN];
double balance; //[BALANCE_LEN];
char lastPay[ARRAY_SIZE];
};

//Prototypes
void setInfo(long);
void display(long);
long search();
void deleteRec(long);
void showAll();

bool rnew = 0;
bool mod = 0;

int main()
{
long fpos;
int choice = 6;
char YorN;
do
{
cout << "\n * * * * M A I N M E N U * * * * * * * \n\n";
cout << "1. Enter a new Customer Account\n";
cout << "2. Display a Customer Account\n";
cout << "3. Delete a Customer Account \n";
cout << "4. Change a Customer Account\n";
cout << "5. Exit the program\n\n";

// Determine user's choice
do
{
cout << "Enter your choice (1-6): ";
cin >> choice;
} while (choice < 1 || choice > 6);

//Process user's choice
switch (choice)
{
case 1:
cin.get();
cout << "\nYou selected Enter a new Customer Account.\n\n";
rnew = 1;
setInfo(0);
rnew = 0;
break;
case 2:
cout << "\nYou selected Display a Customer Account.\n\n";
fpos = search();
if (fpos != -1)
{
display(fpos);
}
else
cout << "\nRecord not found.";
break;
case 3:
cout << "\nYou selected Delete a Customer Account.\n\n";
fpos = search();
if (fpos != -1)
{
display(fpos);
cout << "\nARE YOU SURE YOU WANT TO DELETE THIS RECORD? (Y/N)";
cin >> YorN;
YorN = toupper(YorN);
if (YorN == 'Y')
{
deleteRec(fpos);
break;
}
else
{
cout << "\nRecord was not deleted.\n";
}
}
else
cout << "\nRecord not found.\n";
break;
case 4:
cout << "\nYou selected Change a Customer Account.\n\n";
fpos = search();
if (fpos != -1)
{
cout << "'nRECORD CONTENTS: \n";
display(fpos);
cout << "\nENTER NEW CONTENTS: \n";
setInfo(fpos);
mod = 0;
}
else
cout << "\nRecord not found.\n";
break;
case 5:
cout << "\nYou selected Change a Customer Account.\n\n";
showAll();
break;
case 6:
exit(0);
break;
default: //Anything not between 1-5
break;
} // End switch
} while (choice != 6);
return 0;
}// End of Main

// Function definitions

// setInfo *
// Get info for customer record and write to file. *

void setInfo(long fp)
{
Customer c;
int valid;

do
{
valid = 1;
cout << "\nPlease enter the following information:\n";
cout << "\nCustomer Name: ";
cin.getline(c.name, 45);
cout << "\nCustomer Address: ";
cin.getline(c.address, 45);
cout << "\nCity: ";
cin.getline(c.city, 20);
cout << "\nState: ";
cin.getline(c.state, 4);
cout << "\nZip: ";
cin.getline(c.zip, 11);
cout << "\nTelephone: ";
cin.getline(c.phone, 14);
cout << "\nAccount Balance: ";
cin >> c.balance;
cin.get();
cout << "\nDate of last payment: ";
cin.getline(c.lastPay, 11);

if (strlen(c.name) ==0 || strlen(c.address) ==0 || strlen(c.city) ==0 || strlen(c.state) ==0 || strlen(c.zip) ==0 || strlen(c.lastPay) ==0)
{
cout << "You must enter infroamtion for each field. \n";
valid = 0;
}
if (c.balance < 0)
{
cout << "Please enter 0 or greater for account balance. \n";
valid = 0;
}
} while (!valid);

if (rnew)
{
custFile.open("cust.dat", ios::out | ios::app | ios ::binary );
}
else if (mod)
{
custFile.open("cust.dat", ios::out | ios::app | ios ::binary );
custFile.seekp(fp, ios::beg);
}
if (custFile.fail())
{
cout << "\nError opening file.\n";
return;
}
custFile.write(reinterpret_cast<char*>(&c), sizeof(c));
if (custFile.fail())
{
cout << "\nError writing record to file. \n";
custFile.clear();
custFile.close();
return;
}
}


// Display *
// display record at a given file position. *

void display(long fp)
{
Customer c;
custFile.open("cust.dat", ios::in | ios::binary);
if (custFile.fail())
{
cout << "\nError opening file. \n";
return;
}
if (custFile.peek() == EOF)
{
cout << "\nFile is empty.\n";
custFile.clear();
custFile.close();
return;
}
custFile.seekg(fp, ios::beg);
custFile.read(reinterpret_cast<char*>(&c), sizeof(Customer));

cout.precision(2);
cout.setf(ios::fixed | ios::showpoint);
cout << "Customer Name: " << c.name << endl;
cout << "Customer Address: " << c.address << endl;
cout << "City: " << c.city << endl;
cout << "State: " << c.state << endl;
cout << "Zip: " << c.zip << endl;
cout << "Telephone: " << c.phone << endl;
cout << "Account Balance: " << c.balance << endl;
cout << "Date of last payment: " << c.lastPay << endl;
custFile.clear();
custFile.close();
cout << "\nPress Enter to continue. ";
cin.get();
}


// search
// Returns file pointer position
// of a customer record

long search()
{
char name[45];
Customer c;
long fp;
cout << "\nEnter all or part of the Customer's Name: ";
cin.ignore();
cin.getline(name, 45);
if (name[0] == '\0') //if nothing is entered do nothing
{
return -1;
}

while (true)
{
custFile.read(reinterpret_cast<char*>(&c), sizeof(Customer)); //Read one record
if (custFile.fail())
break;
if (strstr(c.name,name) != NULL) // If search value matches customers name..
{
fp = custFile.tellg();
custFile.clear();
custFile.close();
return (fp - sizeof(c));
}
}
cout << "Record not Found\n";
custFile.clear();
custFile.close();
return -1;
}

// ShowAll *
// Show records in file *

void showAll()
{
cin.ignore();
Customer c;
int count = 0;
custFile.open("cust.dat", ios::in | ios::binary);
if (custFile.fail())
{
cout << "\nError opening file. \n";
return;
}
cout << "\n***Begin Customer Record***\n\n";

while (custFile.peek()!=EOF)
{
custFile.read(reinterpret_cast<char*>(&c), sizeof(Customer));
cout << setprecision(2);
cout << fixed << showpoint;
cout << "\nRECORD NUMBER " << ++count << ":" << endl;
cout << "\nCustomer name: " << c.name << endl;
cout << "Customer Address: " << c.address << endl;
cout << "City: " << c.city << endl;
cout << "State: " << c.state << endl;
cout << "Zip: " << c.zip << endl;
cout << "Telephone: " << c.phone << endl;
cout << "Account Balance: " << c.balance << endl;
cout << "Date of Last Payment: " << c.lastPay << endl;
cout << endl;
cout << "\nPress Enter to Continue...";
cin.get();
}
if (count == 0)
{
cout << "\nFile is empty." << endl;
}
cout << "\n***End of Customer Record Listing***\n\n";
custFile.clear();
custFile.close();
}


// deleteREc *
// This function marks a record for deletion by placing *

void deleteRec(long fp)
{
Customer c;
int Rec = 0;
custFile.open("cust.dat", ios::in | ios::binary);
if (custFile.fail())
{
cout << "\nError opening file.\n";
return;
}

//Mark the file at offset fp for deletion
strcpy_s(c.name, "\0"); //Indicates deletion of record
custFile.seekp(fp, ios::beg);
custFile.write(reinterpret_cast<char*>(&c), sizeof(c));
custFile.clear();
custFile.close();

//Copy customer file to temporary file
custFile.open("cust.dat", ios::in | ios::binary);
tempFile.open("temp.dat", ios::out | ios::binary);
while (custFile.peek() != EOF)
{
custFile.read(reinterpret_cast<char*>(&c), sizeof(c));
tempFile.write(reinterpret_cast<char*>(&c), sizeof(c));
}
custFile.clear();
tempFile.clear();
custFile.close();
tempFile.close();

// Copy temporary file to customer file, skipping
// the records that are marked for deletion
tempFile.open("temp.dat", ios::out | ios::binary);
custFile.open("cust.dat", ios::out | ios::binary);
while (true)
{
tempFile.read(reinterpret_cast<char*>(&c), sizeof(c));
if (tempFile.fail())
break;
if (c.name[0] != '\0')
{
custFile.write(reinterpret_cast<char*>(&c), sizeof(c));
}
}
tempFile.clear();
tempFile.close();
custFile.clear();
custFile.close();
cout << "\nDeletion Successful. \n";
}
Last edited on
Could you be more specific please. "Has errors" doesn't give any indication of whether the code won't compile, or it runs but gives incorrect output or something else.
Well It wouldn't let me type anything else anymore thats why I wasn't specific. The Program is supposed to be a Customer Account. I am supposed to add, edit, and delete any account. As well as look it up and display the information. It lets me input all of the information but it does not seem to retain it. When I try to display it, it simply doesn't display it. And when I search for the name of the Customer It does not find it. Im thinking something is probably wrong with my code.

Instructions are:
Design a generic class or structure to store the following information about a customer account:
Name
Address
City , State , and Zip
Telephone Number
Account balance
Date of last payment
The class or structure should be used to store customer account recors in a file. The program should have a menu that lets the user perform the following operations:
Enter new records into the file
Search for a particular customer’s record and display it.
Search for a particular customer’s record and delete it.
Search for a particular customer’s record and change it.
Display the contents of the entire file
Input validation: When the information for new account is entered, be sure the user enters data for all the fields. No negative account balance should be entered.
Well, ok I had a brief look at this, there's quite a lot of code here. I only got as far as option 1 - which doesn't work. That's fair enough - its the reason why the question was asked. My main advice for now is to focus attention on just a single option at a time. There's no need to be distracted by the rest of the requirements.

So why doesn't option 1 work? There may be more than one issue, but one thing I noticed is that the file was not closed properly after a successful write.

In function setInfo(), after the write() make sure that the file is closed after successfully adding the details.
1
2
3
4
5
6
7
8
9
10
11
    if (custFile.fail())
    {
        cout << "\nError writing record to file. \n";
        custFile.clear();
    }
    else
    {
        cout << "\nRecord was successfully written to file. \n";
    }

    custFile.close();

Topic archived. No new replies allowed.