Aug 31, 2015 at 6:52pm UTC
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <cctype>
using namespace std;
typedef struct
{
char code[20];
char name[30];
char hp[15];
double balance;
}ACCOUNT;
void list(ACCOUNT acc[], int rows);
void search(ACCOUNT acc[], int rows);
void update(ACCOUNT acc[], int rows);
void del(ACCOUNT acc[], int rows);
void transfer(ACCOUNT acc[], int rows);
int main(void)
{
ifstream inFile("programming.txt");
if (!inFile)
cout << "Error opening file\n";
else
{
ACCOUNT ppl[25];
int index = -1;
int selection;
inFile.getline(ppl[++index].code, 20);
while (inFile)
{
if (inFile.peek() == '\n')
inFile.ignore(256, '\n');
inFile.getline(ppl[index].name, 30);
inFile.getline(ppl[index].hp, 15);
inFile >> ppl[index].balance;
inFile >> ppl[++index].code;
}
inFile.close();
//menu start
do
{
cout << "Choose : \n";
cout << "1.List all the account\n";
cout << "2.Search for an account\n";
cout << "3.Deposit/Withdraw\n";
cout << "4.Delete account\n";
cout << "5.Amount Transfer\n";
cout << "6.Exit\n";
cout << "Please make your selection : ";
cin >> selection;
system("cls");
switch (selection)
{
case 1:list(ppl, index);
break;
case 2:search(ppl, index);
break;
case 3:update(ppl, index);
break;
case 4:del(ppl, index);
break;
case 5:transfer(ppl, index);
break;
case 6:
break;
default:cout << "Invalid selection!!System reboot." << endl;
cout << "Please try again." << endl << endl << endl;
break;
return 0;
}
} while (selection != 6);
system("pause");
}
}
void list(ACCOUNT acc[], int rows)
{
cout << " Code\t\t" << "Name\t\t" << "H.P\t\t" << "Balance(RM) " << endl;
for (int i = 0; i < rows; i++)
cout << setw(2) << i + 1 << "." << acc[i].code << "\t" << acc[i].name << "\t" << acc[i].hp << "\t"
<< fixed << setprecision(2) << acc[i].balance << endl;
system("pause");
system("cls");
return;
}
void search(ACCOUNT acc[], int rows)
{
char answer;
char person_name[20];
char coding[20];
cout << "\t\t*****************************" << endl;
cout << "\t\t* This is an advance search *" << endl;
cout << "\t\t*****************************" << endl;
cout << "You want to search by [N]ame or [C]ode ? " << endl;
cout << "Type 'n' to search by name or 'c' to search by code." << endl;
cout << "Type your answer here : ";
fflush(stdin);
cin >> answer;
switch (answer)
{
case 'N':
case 'n':cout << "Enter the person name :";
fflush(stdin);
cin.getline(person_name, 20);
for (int j = 0; j < 20; j++)
{
person_name[j] = toupper(person_name[j]);
}
{
for (int i = 0; i <rows; i++)
{
if (strstr(acc[i].name, person_name))
{
cout << "\n\n\n Code\t\t" << "Name\t\t" << "H.P\t\t" << "Balance(RM) " << endl;
cout << acc[i].code << "\t" << acc[i].name << "\t" << acc[i].hp << "\t" << fixed << setprecision(2) << acc[i].balance << "\n\n\n" << endl;
}
}
}
system("pause");
break;
case 'C':
case 'c':cout << "Enter the code :";
fflush(stdin);
cin.getline(coding, 20);
for (int i = 0; i < rows; i++)
{
if (strstr(acc[i].code, coding))
{
cout << " Code\t\t" << "Name\t\t" << "H.P\t\t" << "Balance(RM) " << endl;
cout << acc[i].code << "\t" << acc[i].name << "\t" << acc[i].hp << "\t" << fixed << setprecision(2) << acc[i].balance << "\n\n\n" << endl;
}
}
system("pause");
break;
default:cout << "Invalid selection!System reboot. \n\n" << endl;
break;
}
}
void update(ACCOUNT acc[], int rows)
{
char acc_num[20];
char reply;
double money;
int found = 0;
string a;
double b;
double c;
cout << "Enter the account number :";
fflush(stdin);
cin.getline(acc_num, 20);
for (int i = 0; i <rows; i++)
{
if (strcmp(acc_num, acc[i].code) == 0)
{
a = acc[i].name;
b = acc[i].balance;
cout << " \n\n\nCode\t\t" << "Name\t\t" << "H.P\t\t" << "Balance(RM) " << endl;
cout << acc[i].code << "\t" << acc[i].name << "\t" << acc[i].hp << "\t" << fixed << setprecision(2) << acc[i].balance << "\n\n\n" << endl;
cout << "You want to [D]eposit or [W]ithdraw ?" << endl;
cout << "Please key in 'd' for deposit or 'w' for withdraw :";
cin >> reply;
switch (reply)
{
case 'D':
case 'd':cout << "Key in the amount you want to deposit : ";
cin >> money;
cout << a << "'s account now have RM" << b + money << "." << "\n\n" << endl;
acc[i].balance += money;
system("pause");
break;
case'W':
case'w':cout << "Key in the amount you want to withdraw : ";
cin >> money;
c = b - money;
if (c < 0)
{
cout << "Sorry, you can't withdraw money more than your balance." << "\n\n\n";
return;
}
else
{
cout << a << "'s account now have RM" << c << "." << "\n\n" << endl;
acc[i].balance -= money;
}
system("pause");
break;
default:cout << "Invalid selection. ";
cout << "system reboot.\n\n";
break;
}
}
else
{
++found;
}
}
if (found == rows)
{
cout << "\n\nInvalid account.System reboot. \n\n\n";
return;
}
}
void del(ACCOUNT acc[], int rows)
{
char code[20];
char answer;
int i = 0;
int found = 0;
cout << "Enter the account number : ";
fflush(stdin);
cin.getline(code, 20);
for (i = 0; i < rows; i++)
{
if (strcmp(code, acc[i].code) == 0)
{
cout << "\n\n\n Code\t\t" << "Name\t\t" << "H.P\t\t" << "Balance(RM) " << endl;
cout << acc[i].code << "\t" << acc[i].name << "\t" << acc[i].hp << "\t" << fixed << setprecision(2) << acc[i].balance << "\n\n\n" << endl;
cout << "Are you sure you want to delete this account ?(Y/N)" << endl;
fflush(stdin);
cin >> answer;
switch (answer)
{
case'y':
case'Y':cout << "Account deleted.\n\n\n" << endl;
system("pause");
break;
case'n':
case'N':cout << "Canceling...Return to the main menu...\n\n\n";
break;
return;
default:cout << "Invalid Key in! System reboot.\n\n\n ";
break;
return;
}
}
else
{
++found;
}
if (found == rows)
{
cout << "\n\n\nInvalid account number.System reboot.\n\n\n";
return;
}
}
}
void transfer(ACCOUNT acc[], int rows)
{
int i = 0;
double f;
string a;
int founded = 0;
int found = 0;
char transferor[20], transferee[20];
cout << "Please enter account nunmber for transferor: ";
fflush(stdin);
cin.getline(transferor, 20);
for (i = 0; i < rows; i++)
{
if (strcmp(transferor, acc[i].code) == 0)
{
f = acc[i].balance;
a = acc[i].name;
cout << "\n\n\n Code\t\t" << "Name\t\t" << "H.P\t\t" << "Balance(RM) " << endl;
cout << acc[i].code << "\t" << acc[i].name << "\t" << acc[i].hp << "\t" << fixed << setprecision(2) << acc[i].balance << "\n\n\n" << endl;
char choice;
cout << "Are you comfirm want to transfer your money?(Y/N)" << endl;
cin >> choice;
switch (choice)
{
case 'y':
case 'Y':cout << "So, now your account balance is " << f - f << "\n" << endl;
acc[i].balance = acc[i].balance - acc[i].balance;
break;
case 'n':
case 'N':cout << "Canceling...Return to main menu...\n\n";
return;
default:cout << "Invalid choice. System reboot.\n\n";
return;
}
}
else
{
++found;
}
if (found == rows)
{
cout << "\n\n\nInvalid account number.System reboot.\n\n\n";
return;
}
}
cout << "Please enter account nunmber for transferee: ";
fflush(stdin);
cin.getline(transferee, 20);
for (i = 0; i < rows; i++)
{
if (strcmp(transferee, acc[i].code) == 0)
{
cout << "\n\n\n Code\t\t" << "Name\t\t" << "H.P\t\t" << "Balance(RM) " << endl;
cout << acc[i].code << "\t" << acc[i].name << "\t" << acc[i].hp << "\t" << fixed << setprecision(2) << acc[i].balance << "\n\n\n" << endl;
cout << a << " have transfer " << f << " to " << acc[i].name << "'s account. " << endl;
cout << "So, now " << acc[i].name << "'s balance is " << acc[i].balance + f << "\n" << endl;
acc[i].balance = acc[i].balance + f;
system("pause");
}
else
{
++founded;
}
if (founded == rows)
{
cout << "\n\n\nInvalid account number.Canceling transfer.System reboot.\n\n\n";
return;
}
}
}
Above is my program about bank account. How i can delete an account in the file ??
Aug 31, 2015 at 9:14pm UTC
the best way is using the method seekg but the format of the file should be in binary as the method will look for a certain size of block in the file to deserialize into an instance of your class or struct. In text file , the way I would do it , which is not optimal like the binary technique using ramdom access , is to keep the stream buffer , which will not contain the stuff you dont want then either trunk the file or place the content into another file . Could be a temp file also.
But definitely for a Bank Exercice , the binary file is the common way. Hope that help .
Sep 1, 2015 at 1:06pm UTC
Another way is to use a Temp file.
This gives you the opportunity to make your changes, view the changes, change your mind and not make the change, or save your changes to the master file.