|
-
May 16th, 2017, 12:10 AM
#1
c++ database.it should let you enter an account but doesnt save what i put in i think
Code:
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstdlib>
#include<cstring>
using namespace std;
const int NAME_SIZE = 25;
const int ADDR_SIZE = 30;
const int CSZ_SIZE = 30;
const int PHONE_SIZE = 15;
const int DLP_SIZE = 20;
int NUM_RECORDS = 0;
void write_account();
void display(int num);
void display_all();
void display_sp(int num);
void delete_account(int num);
struct Account{
char name[NAME_SIZE];
char addr[ADDR_SIZE];
char csz[CSZ_SIZE];
char phone[PHONE_SIZE];
double balance;
char dlp[DLP_SIZE];
};
int main()
{
char ch;
int num;
do
{
cout<<"\n**********************MAIN MENU*********************************";
cout<<"\n1. Enter a New Customer Account";
cout<<"\n2. Display a Customer Account";
cout<<"\n3. Delete a Customer Account";
cout<<"\n4. Change a customer account";
cout<<"\n5. Display All Customer Accounts";
cout<<"\n6. EXIT THE PROGRAM";
cout<<"\nEnter Your Option (1-6) ";
cin>>ch;
switch(ch)
{
case '1':
write_account();
break;
case '2': // display account number
cout<<"\n\n\tEnter The account No. : ";
cin>>num;
display(num);
break;
case '3':
cout<<"\n\n\tEnter The account No. : ";
cin>>num;
delete_account(num);
break;
case '4':
cout<<"\n\n\tEnter The account No. : ";
cin>>num;
display_sp(num);
break;
case '5':
display_all();
break;
case '6':
cout<<"\n\n\tThank you come again";
break;
default :cout<<"\a";
}
cin.ignore();
cin.get();
}while(ch!='6');
return 0;
}
void write_account()
{
Account ac = {"","","","",0.0,""};
double tempBalance;
cout << "Name: ";
cin.ignore();
cin.getline(ac.name, NAME_SIZE);
cout << "Address: ";
cin.getline(ac.addr, ADDR_SIZE);
cout << "City, State, ZIP ";
cin.getline(ac.csz, CSZ_SIZE);
cout << "PHONE # : ";
cin.getline(ac.phone, PHONE_SIZE);
cout << "Balance: ";
cin >> tempBalance;
if (tempBalance < 0 ){
ac.balance = tempBalance;
}
cout << "Date of Last Payment: ";
cin.getline(ac.dlp, DLP_SIZE);
fstream customers("cust.dat", ios::app | ios::binary);
customers.write(reinterpret_cast<char *> (&ac), sizeof(ac));
customers.close();
NUM_RECORDS++;
}
void display(int num){
Account record;
fstream inventory("cust.dat", ios::in | ios::binary);
// Now read and display the records.
inventory.seekg(num* sizeof(record), ios::beg);
inventory.read(reinterpret_cast<char *>(&record), sizeof(record));
cout<< "Name: " << record.name << endl;
cout<< "Address: " << record.addr << endl;
cout<< "City, State, Zip: " << record.csz << endl;
cout<< "Phone Number: " << record.phone << endl;
cout<< "Current Balance: $" << record.balance << endl;
cout<< "Date of Last Payment " << record.dlp << endl;
// Close the file.
inventory.close();
}
void display_all(){
cout<< "\n";
cout << "Displaying all records."<<endl;;
Account record;
fstream inventory("cust.dat", ios::in | ios::binary);
// Now read and display the records.
inventory.read(reinterpret_cast<char *>(&record), sizeof(record));
while(!inventory.eof()){
cout<< "Name: " << record.name << endl;
cout<< "Address: " << record.addr << endl;
cout<< "City, State, Zip: " << record.csz << endl;
cout<< "Phone Number: " << record.phone << endl;
cout<< "Current Balance: $" << record.balance << endl;
cout<< "Date of Last Payment " << record.dlp << endl;
cout<< "\n";
inventory.read(reinterpret_cast<char *>(&record), sizeof(record));
}
// Close the file.
inventory.close();
}
void display_sp(int num){
Account record;
fstream inventory("cust.dat", ios::in | ios::out | ios::binary);
// Now read and display the records.
inventory.seekg(num* sizeof(record), ios::beg);
inventory.read(reinterpret_cast<char *>(&record), sizeof(record));
double tempBalance;
cout << "Name: ";
cin.ignore();
cin.getline(record.name, NAME_SIZE);
cout << "Address: ";
cin.getline(record.addr, ADDR_SIZE);
cout << "City, State, ZIP ";
cin.getline(record.csz, CSZ_SIZE);
cout << "PHONE # : ";
cin.getline(record.phone, PHONE_SIZE);
cout << "Balance: ";
cin >> tempBalance;
if (tempBalance > 0 ){
record.balance = tempBalance;
}
cout << "Date of Last Payment: ";
cin.getline(record.dlp, DLP_SIZE);
inventory.seekp(num * sizeof(record), ios::beg);
inventory.write(reinterpret_cast<char *>(&record), sizeof(record));
// Close the file.
inventory.close();
}
void delete_account(int num){
Account record;
fstream inventory;
fstream tempFile;
inventory.open("cust.dat", ios::in | ios::out | ios::binary);
if (inventory.fail())
{
cout << "\nError opening file.\n";
inventory.close();
exit(0);
}
// Now read and display the records.
strcpy(record.name, "\0");
inventory.seekp(num, ios::beg);
inventory.write(reinterpret_cast<char *>(&record),sizeof(record));
inventory.close();
inventory.open("cust.dat", ios::in | ios::binary);
tempFile.open("temp.dat", ios::out | ios::binary);
inventory.read(reinterpret_cast<char *>(&record),sizeof(record));
while (inventory)
{
tempFile.write(reinterpret_cast<char *>(&record),sizeof(record));
inventory.read(reinterpret_cast<char *>(&record),sizeof(record));
}
inventory.close();
tempFile.close();
tempFile.open("temp.dat", ios::in | ios::binary);
inventory.open("cust.dat", ios::out | ios::binary);
tempFile.read(reinterpret_cast<char *>(&record),sizeof(record));
while (!tempFile.eof())
{
if(record.name[0] != '\0')
{
inventory.write(reinterpret_cast<char *>(&record),sizeof(record));
}
tempFile.read(reinterpret_cast<char *>(&record),sizeof(record));
}
tempFile.close();
inventory.close();
cout << "\nDeletion successful.\n";
}
Last edited by 2kaud; May 16th, 2017 at 01:48 AM.
Reason: Added code tags
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|