1. Enter in new record.
2. Display an Individual Record.
3. Delete an Individual Record.
4. Change/Update an Individual Record.
5. Display Contents of Entire File.
Well since we are using, I guess you could say, advance file I/O, my question is HOW THE HECK DO YOU search for an individual c-string records??? It seems very intuitive to just ask the user for the "numeric" record, say 1, and it will display it. However, in the real-world banks do not access individual records in that fashion due to thousands of records. So, so far we have all options defined. Option 3 has not been worked with yet due to the fact its not working correctly. I was thinking of creating a character variable inside option 2, 3, and 4 and apply that variable into seekg, but you can't do that. It goes into a infinite loop. I guess because we are writing into binary format. If you have steady hands you can stop the scroll bar in the command prompt and see the information it spits out right before the infinite loop, hahahaha
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cctype>
using namespace std;
const int NAME = 51;
const int ADDRESS = 51;
const int HOMETOWN = 51;
const int TELEPHONE = 14;
const int LAST_PAYMENT = 11;
struct Account
{
char name[NAME];
char address[ADDRESS];
char cityStateZip[HOMETOWN];
char teleNumber[TELEPHONE];
char lastPayment[LAST_PAYMENT];
float accountBalance;
Account()
{
//Empty on purpose
}
};
//FUNCTION PROTOTYPES
void displayMenu();
int getChoice();
void newRecords(Account&);
void displayIndividualRecord(Account);
/*void deleteIndividualRecord(Account);*/
void changeIndividualRecord(Account&);
void displayFile(Account);
int main()
{
Account information;
int option;
do
{
displayMenu();
option = getChoice();
switch(option)
{
case 1: newRecords(information);
break;
case 2: displayIndividualRecord(information);
break;
/*case 3: deleteIndividualRecord(information);
break;*/
case 4: changeIndividualRecord(information);
break;
case 5: displayFile(information);
break;
}
}while(option != 6);
cout << "Have a nice day!\n\n";
return 0;
}
/***************************************************
*
*
*
*
***************************************************/
void displayMenu()
{
cout << "\n\tWachovia Data Base\n\n";
cout << "1. Enter new records into file.\n";
cout << "2. Display individual record.\n";
cout << "3. Delete individual record.\n";
cout << "4. Change individual record.\n";
cout << "5. Display contents of entire file.\n";
cout << "6. Exit.\n";
}
/***************************************************
*
*
*
*
***************************************************/
int getChoice()
{
int choice;
cout << "\nWhat option would you like to work with? ";
cin >> choice;
while(choice < 1 || choice > 6)
{
cout << "Invalid! Only valid choices are 1-6. Please re-enter. ";
cin >> choice;
}
return choice;
}
/***************************************************
*
*
*
*
***************************************************/
void newRecords(Account &customer)
{
char response;
//Create file object and open file
fstream citizen("DataBase.txt", ios::out | ios::app /*| ios::binary*/);
//Test for file open error
if(!citizen)
{
cout << "Error opening file. Shutting down.\n";
exit(0);
}
do
{
//If file opens correctly get information entered by user
//and write information to the file in binary mode.
cout << "Enter the following information:\n\n";
cout << "Name: ";
cin.ignore();
cin.getline(customer.name, NAME);
cout << "Address: ";
cin.getline(customer.address, ADDRESS);
cout << "City, State, and Zip: ";
cin.getline(customer.cityStateZip, HOMETOWN);
cout << "Telephone Number: ";
cin.getline(customer.teleNumber, TELEPHONE);
cout << "Account Balance: ";
cin >> customer.accountBalance;
while(customer.accountBalance < 0.00)
{
cout << "Invalid! Customer's account balance must be greater\n"
<< "than or equal to $0.00. Please re-enter in customer's\n"
<< "account balance:\n";
cin >> customer.accountBalance;
}
cout << "Last Payement(mm/dd/yyyy): ";
cin.ignore();
cin.getline(customer.lastPayment, LAST_PAYMENT);
citizen.write(reinterpret_cast<char *>(&customer),
sizeof(customer));
cout << "\n\nDo you want to enter in another record(Y/N)? ";
cin >> response;
cin.ignore();
}while(toupper(response) == 'Y');
citizen.close();
}
/***************************************************
*
*
*
*
***************************************************/
void displayIndividualRecord(Account customer)
{
long record;
//Open file
fstream citizen("DataBase.txt", ios::in | ios::binary);
//Test for file open error
if(!citizen)
{
cout << "Error opening file. Shutting down.\n";
exit(0);
}
else
{
cout << "\nWhat record would you like to look at? ";
cin >> record;
cout << endl;
citizen.seekg(record * sizeof(customer), ios::beg);
citizen.read(reinterpret_cast<char *>(&customer), sizeof(customer));
cout << "\n\nName: " << customer.name << endl;
cout << "Address: " << customer.address << endl;
cout << "City, State, and Zip: " << customer.cityStateZip << endl;
cout << "Telephone Number: " << customer.teleNumber << endl;
cout << fixed << showpoint << setprecision(2);
cout << "Account Balance: " << "$" << customer.accountBalance << endl;
cout << "Last Payment(mm/dd/yyyy): " << customer.lastPayment << endl << endl;
}
citizen.close();
}
/***************************************************
*
*
*
*
***************************************************
void deleteIndividualRecord(Account customer)
{
long record;
//Open file
fstream citizen("Customer.txt", ios::in | ios::binary);
if(!citizen)
{
cout << "Error opening file.\n";
exit(0);
}
cout << "What record would you like to delete? ";
cin >> record;
cout << endl;
//delete citizen.seekg(record * sizeof(customer), ios::beg);
cout << "Deleted record " << record << "." << endl;
citizen.close();
/*char search[NAME];
char *strPtr = NULL;
cout << "Enter a Name to searh for: (Capital letter first) ";
cin.getline(search, NAME);
int i = 0;
while(i < 3)
{
strPtr = strstr([i].name, search);
if (strPtr != NULL)
{
cout << info[i].name << endl;
}
i++;
}
char response;
//Create file object and open file to display customer information
fstream citizen("Customers.txt", ios::in | ios::binary);
//Test for file open error
if(!citizen)
{
cout << "Error opening file. Shutting down.\n";
exit(0);
}
else
{
cout << "Wachovia Customers:\n\n";
//Read one structure at a time and echo to screen.
citizen.read(reinterpret_cast<char *>(&customer),
sizeof(customer));
while(!citizen.eof())
{
cout << "Name: " << customer.name << endl;
cout << "Address: " << customer.address << endl;
cout << "City, State, and Zip: " << customer.cityStateZip << endl;
cout << "Telephone Number: " << customer.teleNumber << endl;
cout << fixed << showpoint << setprecision(2);
cout << "Account Balance: " << "$" << customer.accountBalance << endl;
cout << "Date of Last Payment(mm/dd/yyyy): " << customer.lastPayment << endl;
cout << "\nStrike any key to see the next record.\n";
cin.get(response);
citizen.read(reinterpret_cast<char *>(&customer),
sizeof(customer));
}
cout << "That is all the information in the file.\n";
}
} */
/***************************************************
*
*
*
*
***************************************************/
void changeIndividualRecord(Account &customer)
{
long record;
char response;
//Open file
fstream citizen("DataBase.txt", ios::in | ios::out | ios::binary);
if(!citizen)
{
cout << "Error opening file.\n";
exit(0);
}
cout << "\nWhat record would you like to change? ";
cin >> record;
citizen.seekg(record * sizeof(customer), ios::beg);
citizen.read(reinterpret_cast<char *>(&customer), sizeof(customer));
//Old information
cout << "\n\nName: " << customer.name << endl;
cout << "Address: " << customer.address << endl;
cout << "City, State, and Zip: " << customer.cityStateZip << endl;
cout << "Telephone Number: " << customer.teleNumber << endl;
cout << fixed << showpoint << setprecision(2);
cout << "Account Balance: " << "$" << customer.accountBalance << endl;
cout << "Last Payment(mm/dd/yyyy): " << customer.lastPayment << endl;
cout << "\nEnter new information for customer:\n";
cout << "Do you want to update the customer's name(Y/N)? ";
cin >> response;
if(toupper(response) == 'Y')
{
cout << "Name: ";
cin.get();
cin.getline(customer.name, NAME);
}
else
cout << "Customer's name has not been updated.";
cout << "\n\nDo you want to update the customer's address(Y/N)? ";
cin >> response;
if(toupper(response) == 'Y')
{
cout << "Address: ";
cin.get();
cin.getline(customer.address, ADDRESS);
}
else
cout << "Customer's address has not be updated.";
cout << "\n\nDo you want to update the City, State, and Zip code(Y/N)? ";
cin >> response;
if(toupper(response) == 'Y')
{
cout << "City, State, and Zip: ";
cin.get();
cin.getline(customer.cityStateZip, HOMETOWN);
}
else
cout << "City, State, and Zip code has not be updated for this customer.";
cout << "\n\nDo you want to update the customer's telephone number(Y/N)? ";
cin >> response;
if(toupper(response) == 'Y')
{
cout << "Telephone Number: ";
cin.get();
cin.getline(customer.teleNumber, TELEPHONE);
}
else
cout << "Customer's telephone number has not been updated.";
cout << "\n\nDo you want to update the customer's account balance(Y/N)? ";
cin >> response;
if(toupper(response) == 'Y')
{
cout << "Account Balance: ";
cin.get();
cin >> customer.accountBalance;
while(customer.accountBalance < 0.00)
{
cout << "Invalid! Customer's account balance must be greater\n"
<< "than or equal to $0.00. Please re-enter in customer's\n"
<< "account balance:\n";
cin >> customer.accountBalance;
}
}
else
cout << "Customer's account balance has not been updated.";
cout << "\n\nDo you want to update date of last payment the customer made(Y/N)? ";
cin >> response;
if(toupper(response) == 'Y')
{
cout << "Last Payement(mm/dd/yyyy): ";
cin.get();
cin.getline(customer.lastPayment, LAST_PAYMENT);
}
else
cout << "Date of last payment has not been updated.\n\n";
citizen.seekp(record * sizeof(customer), ios::beg);
citizen.write(reinterpret_cast<char *>(&customer), sizeof(customer));
//Close file
citizen.close();
}
/***************************************************
*
*
*
*
***************************************************/
void displayFile(Account customer)
{
//Create file object and open file to display customer information
fstream citizen("DataBase.txt", ios::in | ios::binary);
//Test for file open error
if(!citizen)
{
cout << "Error opening file. Shutting down.\n";
exit(0);
}
else
{
cout << "\n\tWachovia Customers:\n";
//Read one structure at a time and echo to screen.
citizen.read(reinterpret_cast<char *>(&customer),
sizeof(customer));
while(!citizen.eof())
{
cout << "\nName: " << customer.name << endl;
cout << "Address: " << customer.address << endl;
cout << "City, State, and Zip: " << customer.cityStateZip << endl;
cout << "Telephone Number: " << customer.teleNumber << endl;
cout << fixed << showpoint << setprecision(2);
cout << "Account Balance: " << "$" << customer.accountBalance << endl;
cout << "Last Payment(mm/dd/yyyy): " << customer.lastPayment << endl;
citizen.read(reinterpret_cast<char *>(&customer),
sizeof(customer));
}
cout << "\nThat is all the information in the file.\n";
}
citizen.close();
}

New Topic/Question
Reply




MultiQuote





|