me and my partner are having a tough time trying to find out how to fix the error on our code. Were engineering undergrads and programming isn't exactly our forte so please take it easy on us.
Im sorry if I'm sending the whole code, but Im quite desperate since we need this project to pass the course. Thanks. Here is my code:
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <cctype>
#include <iomanip>
using namespace std;
struct baccount {
int pin;
char accholder[50];
int deposit;
void create_account();
void display_account() const;
void modify();
void depmoney(int);
void drawmoney(int);
void report() const;
int retpin() const;
int retdeposit() const;
};
void create_account()
{
struct baccount qeqe;
cout << "\nEnter Pin Number:";
cin >> qeqe.pin;
cout << "\n\nEnter your name : ";
cin.ignore();
cin.getline(qeqe.accholder, 50);
do {
cout << "\nEnter the initial amount to be stored in the account(Minimum P2000) : ";
cin >> qeqe.deposit;
} while (qeqe.deposit < 2000);
cout << "\n\n\nAccount Created..";
};
void display_account()
{
struct baccount qeqe;
cout << "\nPin Number: " << qeqe.pin;
cout << "\nAccount Holder Name : ";
cout << qeqe.accholder;
cout << "\nCurrent Balance : " << qeqe.deposit;
}
void depmoney(int x)
{
struct baccount qeqe;
qeqe.deposit += x;
}
void modify()
{
struct baccount qeqe;
cout << "\nPin Number : " << qeqe.pin;
cout << "\nModify Account Holder Name : ";
cin.ignore();
cin.getline(qeqe.accholder, 50);
cout << "\nModify Balance amount : ";
cin >> qeqe.deposit;
}
void drawmoney(int x)
{
struct baccount qeqe;
qeqe.deposit -= x;
}
void report()
{
struct baccount qeqe;
cout << qeqe.pin << setw(15) << " " << qeqe.accholder << setw(15) << " " << setw(8) << qeqe.deposit << endl;
}
int retpin()
{
struct baccount qeqe;
return qeqe.pin;
}
int retdeposit()
{
struct baccount qeqe;
return qeqe.deposit;
}
void write_account();
void display_sp(int);
void modify_account(int);
void delete_account(int);
void display_all();
void deposit_withdraw(int, int);
int main()
{
char choice;
int pnum;
do {
system("cls");
cout << "Welcome to Cutaran-Eleria Bank" << endl;
cout << "[1] Create new account" << endl;
cout << "[2] Deposit" << endl;
cout << "[3] Withraw" << endl;
cout << "[4] Show current balance" << endl;
cout << "[5] Check list of accounts" << endl;
cout << "[6] Remove an account" << endl;
cout << "[7] Edit an account" << endl;
cout << "[8] Exit Cutaran-Eleria Bank" << endl;
cout << "Please enter the command you want to do: " << endl;
cin >> choice;
system("cls");
switch (choice) {
case '1':
write_account();
break;
case '2':
cout << "\n\n\tEnter Pin Number: ";
cin >> pnum;
deposit_withdraw(pnum, 1);
break;
case '3':
cout << "\n\n\tEnter The account No. : ";
cin >> pnum;
deposit_withdraw(pnum, 2);
break;
case '4':
cout << "\n\n\tEnter The account No. : ";
cin >> pnum;
display_sp(pnum);
break;
case '5':
display_all();
break;
case '6':
cout << "\n\n\tEnter Pin Number : ";
cin >> pnum;
delete_account(pnum);
break;
case '7':
cout << "\n\n\tEnter Pin Number. : ";
cin >> pnum;
modify_account(pnum);
break;
case '8':
cout << "\n\nThank you for using Cutaran-Eleria Bank";
break;
default:
cout << "\a";
}
cin.ignore();
cin.get();
} while (choice != '8');
return 0;
}
void write_account()
{
struct baccount qeqe;
ofstream newrecord;
newrecord.open("accounts.dat", ios::binary | ios::app);
qeqe.create_account();
newrecord.write(reinterpret_cast<char*>(&qeqe), sizeof(baccount));
newrecord.close();
cout << "Your account is now saved into the system.";
}
void display_sp(int n)
{
baccount bankacc;
bool flag = false;
ifstream oldrecords;
oldrecords.open("accounts.dat", ios::binary);
if (!oldrecords) {
cout << "The account is not in the system";
return;
}
cout << "\nCurrent Balance\n";
while (oldrecords.read(reinterpret_cast<char*>(&bankacc), sizeof(baccount))) {
if (bankacc.retpin() == n) {
bankacc.display_account();
flag = true;
}
}
oldrecords.close();
}
void modify_account(int n)
{
bool found = false;
baccount acc;
fstream File;
File.open("accounts.dat", ios::binary | ios::in | ios::out);
if (!File) {
cout << "Unable to open account.";
return;
}
while (!File.eof() && found == false) {
File.read(reinterpret_cast<char*>(&acc), sizeof(baccount));
if (acc.retpin() == n) {
acc.display_account();
cout << "\n\nEnter The New Details of account" << endl;
acc.modify();
int pos = (-1) * static_cast<int>(sizeof(baccount));
File.seekp(pos, ios::cur);
File.write(reinterpret_cast<char*>(&acc), sizeof(baccount));
cout << "\n\n\t Record Updated";
found = true;
}
}
File.close();
if (found == false)
cout << "\n\n Record Not Found ";
}
void delete_account(int n)
{
baccount acc;
ifstream oldrecords;
ofstream newrecord;
oldrecords.open("accounts.dat", ios::binary);
if (!oldrecords) {
cout << "File could not be opened!!! Press any Key...";
return;
}
newrecord.open("Temp.dat", ios::binary);
oldrecords.seekg(0, ios::beg);
while (oldrecords.read(reinterpret_cast<char*>(&acc), sizeof(baccount))) {
if (acc.retpin() != n) {
newrecord.write(reinterpret_cast<char*>(&acc), sizeof(baccount));
}
}
oldrecords.close();
newrecord.close();
remove("accounts.dat");
rename("Temp.dat", "accounts.dat");
cout << "\n\nRecord Deleted ..";
}
void display_all()
{
baccount acc;
ifstream oldrecords;
oldrecords.open("accounts.dat", ios::binary);
if (!oldrecords) {
cout << "Unable to show accounts";
return;
}
cout << "\n\n\t\tList of Accounts\n\n";
cout << "Pin Number Name CurrentBalance\n";
while (oldrecords.read(reinterpret_cast<char*>(&acc), sizeof(baccount))) {
acc.report();
}
oldrecords.close();
}
void deposit_withdraw(int n, int option)
{
int amt;
bool found = false;
baccount acc;
fstream File;
File.open("accounts.dat", ios::binary | ios::in | ios::out);
if (!File) {
cout << "Unable to access the account.";
return;
}
while (!File.eof() && found == false) {
File.read(reinterpret_cast<char*>(&acc), sizeof(baccount));
if (acc.retpin() == n) {
acc.display_account();
if (option == 1) {
cout << "\n\nEnter The amount to be deposited";
cin >> amt;
acc.depmoney(amt);
}
if (option == 2) {
cout << "\n\nEnter The amount to be withdrawn";
cin >> amt;
int bal = acc.retdeposit() - amt;
if (amt > acc.retdeposit())
cout << "Insufficient balance";
else
acc.drawmoney(amt);
}
int pos = (-1) * static_cast<int>(sizeof(acc));
File.seekp(pos, ios::cur);
File.write(reinterpret_cast<char*>(&acc), sizeof(baccount));
cout << "\n\nRecord Updated";
found = true;
}
}
File.close();
if (found == false)
cout << "\n\n Record Not Found ";
}