Im Stuck

Need help with breaking up the functions into 4 separate <.h> files.

This is what I have so far and this is the problem that I am working on



Customer Accounts


This program should be designed and written by a team of students.Here are some
suggestions :
• One student should design function main, which will call other program functions.
The remainder of the functions will be designed by other members of the team.
• The requirements of the program should be analyzed so that each student is given
about the same workload.
Write a program that uses a structure to store the following data about a customer
account :
• Name
• Address
• City, State, and ZIP
• Telephone Number
• Account Balance
• Date of Last Payment
The structure should be used to store customer account records 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 data for a new account is entered, be sure the user enters
data for all the fields.No negative account balances should be entered

Complete the problem as stated on Page 717 #16 in your text book. However, include the following:
• Team initials - Team One (TO) and Team Two (TT). If you are assigned to do a module(s) as the 'header' file(s) (.h) then name your file YourTeamInitialsFirstNameLastNamelnitialsModuleName.h

• If you are the team lead then you will integrate all the modules with your .cpp file. Name your file Your TeamInitialsFirstNameLastNamelnitials.cpp

• Team Lead will ZIP the project and upload the zipped file. . Your program should work for no existing input file and for existing input file.

• Add 'custID' to the structure.
• The modules are the following:
o newRecord()
o searchCustByNum)
o searchCustByName()
o deleteCustByNumName()
o updateCustName()
o updateCustAddress()
o deposit Amount() - the customer deposits an amount to their account.
Verify their customer ID and name. Add the deposit amount to their
account.
o withdrawAmount()
• the customer withdraws an amount from their account. Verify their customer ID and name. Verify they have sufficient funds in their account to withdraw. If so, subtract the withdrawal amount from their account. If not, subtract the withdrawal amount plus a overdraft fee of $35 from their account.
o displayAllCust() Name the file Your TeamInitialsFirstNameLastName.zip OR YourTeamInitialsFirstNameLastNameModuleName.h





Last edited on
This is what i have so far

#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
#include <cstdlib>
#include <cstring>
using namespace std;


const int NAME_SIZE = 51; const int ADDR_SIZE = 51; const int CITY_SIZE = 20; const int STATE_SIZE = 15;
const int ZIP_SIZE = 6; const int PHONE_SIZE = 14; const int PAYMENT_DATE_SIZE = 11;
struct Customer
{
char name[NAME_SIZE]; char address[ADDR_SIZE]; char city[CITY_SIZE]; char state[STATE_SIZE];
char zip[ZIP_SIZE]; char phone[PHONE_SIZE]; double acct_balance; char payment_date[PAYMENT_DATE_SIZE];
};
void displayMenu(); int getMenuChoice(); int enterRecords(Customer); int searchName(Customer);
int searchAndDisplay(Customer); int searchAndDelete(Customer); int searchAndChange(Customer);
void showRecord(Customer); int displayContents(Customer);

int main()
{
int choice; char again = 'N'; Customer person = { "" };
do
{
displayMenu(); choice = getMenuChoice();
switch (choice)
{
case 1: enterRecords(person);
break;
case 2: searchAndDisplay(person);
break;
case 3: searchAndDelete(person);
break;
case 4: searchAndChange(person);
break;
case 5: displayContents(person);
break;
case 6: cout << "Quitting program...";
return 0;
default: cout << "That is an invalid choice. ";
}
cout << " Display menu again? Y or N: "; cin >> again;
while (again != 'Y' && again != 'y' && again != 'N' && again != 'n')
{
cout << "Error. Please type either a Y or N: "; cin >> again;
}
cin.ignore();
} while (toupper(again) == 'Y');
cout << "Program terminating...";
return 0;
}

void displayMenu()
{
cout << "(1) Enter new records into the file." << endl; cout << "(2) Search for a record and display it." << endl;
cout << "(3) Search for a record and delete it." << endl; cout << "(4) Search for a record and change it." << endl;
cout << "(5) Display the contents of the entire file." << endl; cout << "(6) Quit." << endl;
}

int getMenuChoice()
{
int choice;
cout << " Menu choice: "; cin >> choice; cin.ignore();
while (choice < 1 || choice > 6)
{
cout << "Error. Please enter a choice between 1 and 6: "; cin >> choice;
}
return choice;
}


Last edited on
int enterRecords(Customer person)
{
char again; fstream records("records.dat", ios::out | ios::app | ios::binary);
if (!records)
{
cout << "Error opening file. Program aborting. "; return 0;
}
do
{
cout << "Enter the following data about a person: ";
cout << "Name: "; cin.getline(person.name, NAME_SIZE);
cout << "Street Address: "; cin.getline(person.address, ADDR_SIZE);
cout << "City: "; cin.getline(person.city, CITY_SIZE);
cout << "State: "; cin.getline(person.state, STATE_SIZE);
cout << "Zip Code: "; cin.getline(person.zip, ZIP_SIZE);
cout << "Telephone number: "; cin.getline(person.phone, PHONE_SIZE);
cout << "Account Balance: $"; cin >> person.acct_balance; cin.ignore();
cout << "Date of last payment (##/##/####): "; cin.getline(person.payment_date, PAYMENT_DATE_SIZE);
// Write the contents of the person structure to the file.
records.write(reinterpret_cast<char *>(&person), sizeof(person));
cout << "Do you want to enter another record? "; cin >> again;
while (again != 'Y' && again != 'y' && again != 'N' && again != 'n')
{
cout << "Error. Please type either a Y or N: "; cin >> again;
}
cin.ignore();
} while (toupper(again) == 'Y');
records.close();
return 1;//Successful
}

int searchName(Customer person)
{
char name[NAME_SIZE]; bool found = false; int pos = 0; fstream records;
records.open("records.dat", ios::in | ios::binary);
if (!records)
{
cout << "Error opening file. Program aborting. "; return 0;
}
cout << "What is the name of the person to search for?: "; cin.getline(name, NAME_SIZE);
records.read(reinterpret_cast<char *>(&person), sizeof(person));
while (!records.eof())
{
if (strcmp(person.name, name) == 0)
{
found = true; break;
}
// If name wasn't found in the first record, read the next record from the file.
records.read(reinterpret_cast<char *>(&person), sizeof(person));
pos++;

}
records.close();
if (found == true)
{
cout << "Record found for " << name << ". Record #" << pos + 1 << endl;
}
else if (found == false)
{
cout << "No record found for " << name << endl; pos = -1;
}
//Return the position in the file the name was found at, or a -1 if the name was not found.
cout << "pos is: " << pos << endl; return pos;
}

int searchAndDisplay(Customer person)
{
long recNum;
cout << "***SEARCH AND DISPLAY***" << endl;
// Call the searchName function and assign it to recNum
recNum = searchName(person);
if (recNum == -1)
{
return 0;
}
fstream records;
records.open("records.dat", ios::in | ios::binary);
if (!records)
{
cout << "Error opening file. Program aborting. "; return 0;
}
//Move to the record and read it
records.seekg(recNum * sizeof(person), ios::beg); records.read(reinterpret_cast<char *>(&person), sizeof(person));
showRecord(person); records.close();
return 1;//Successful
}

int searchAndChange(Customer person)
{
long recNum;
cout << "***SEARCH AND CHANGE***" << endl;
// Call the searchName function and assign it to recNum
recNum = searchName(person);
if (recNum == -1)
{
return 0;
}
fstream records; records.open("records.dat", ios::in | ios::out | ios::binary);
if (!records)
{
cout << "Error opening file. Program aborting. "; return 0;
}
//Move to the record and read it
records.seekg(recNum * sizeof(person), ios::beg); records.read(reinterpret_cast<char *>(&person), sizeof(person));
showRecord(person);
cout << " ENTER NEW DATA: ";
cout << "Name: "; cin.getline(person.name, NAME_SIZE);
cout << "Street Address: "; cin.getline(person.address, ADDR_SIZE);
cout << "City: "; cin.getline(person.city, CITY_SIZE);
cout << "State: "; cin.getline(person.state, STATE_SIZE);
cout << "Zip Code: "; cin.getline(person.zip, ZIP_SIZE);
cout << "Telephone number: "; cin.getline(person.phone, PHONE_SIZE);
cout << "Account Balance: $"; cin >> person.acct_balance; cin.ignore();
cout << "Date of last payment (##/##/####): "; cin.getline(person.payment_date, PAYMENT_DATE_SIZE);
//Move back to the beginning of this record's position.
records.clear(); records.seekp(recNum * sizeof(person), ios::beg);
//Write the new record over the current record.
records.write(reinterpret_cast<char *>(&person), sizeof(person));
records.close();
return 1;//Successful
}

int searchAndDelete(Customer person)
{
long recNum; int count = -1;
cout << "***SEARCH AND DELETE***" << endl;
// Call the searchName function and assign it to recNum
recNum = searchName(person);
if (recNum == -1) // If name was not found, break out of this function
{
return 0;
}
fstream tempFile; tempFile.open("temp.dat", ios::out | ios::binary); fstream records;
records.open("records.dat", ios::in | ios::binary);
if (!records)
{
cout << "Error opening file. Program aborting. "; return 0;
}
int deleteLocation = recNum * sizeof(person);
int currLoc = 0;
// Move to the record and read it

records.read(reinterpret_cast<char *>(&person), sizeof(person));//Read a record
count++;
while (!records.eof())
{
if (deleteLocation != count)
{
tempFile.write(reinterpret_cast<char*>(&person), sizeof(person));
}
records.read(reinterpret_cast<char *>(&person), sizeof(person));//Read a record
count++;//A record read
}
tempFile.close(); records.close();
remove("records.dat");
rename("temp.dat", "records.dat");
return 1;//Successful
}

void showRecord(Customer person)
{
cout << " Name: " << person.name << endl;
cout << "Address: " << person.address << endl;
cout << "City: " << person.city << endl;
cout << "State: " << person.state << endl;
cout << "Zip: " << person.zip << endl;
cout << "Phone: " << person.phone << endl;
cout << "Account Balance: $" << person.acct_balance << endl;
cout << "Last payment date: " << person.payment_date << endl;
}

int displayContents(Customer person)
{
fstream records; records.open("records.dat", ios::in | ios::binary);
if (!records)
{
cout << "Error opening file. Program aborting. "; return 0;
}
cout << " FILE CONTENTS: ";
// Read the first record from the file.
records.read(reinterpret_cast<char *>(&person), sizeof(person));
while (!records.eof())
{
showRecord(person); records.read(reinterpret_cast<char *>(&person), sizeof(person));
}
cout << " END OF FILE REACHED. "; records.close();
return 1;//Successful
}
Topic archived. No new replies allowed.