The commands write_ account () is supposed to input the lines “account.dat” file while doing the access create_ account() in the class amount. I want to an imput of multiple arguments of the create_ account in the “account.dat” file then do the display of this files on the screen using the command display_ sp () and the show_ account () is the file.write wrong? Am interested in making this happen in a binary. Can it be done?
Am actually not able to move beyond (file.good) error.
#include <iostream>
#include <fstream>
usingnamespace std;
class account {
int accno{};
char name[50]{};
int deposit{};
char type{};
public:
void create_account();
int repacno();
void dep(int);
void show_account();
};
void account::create_account() {
cout << "\nEnter Account No: ";
cin >> accno;
cout << "Account Holder Name: ";
cin.ignore();
cin.getline(name, 50);
cout << "Choose type of account: CURRENT/SAVING (C/S): ";
cin >> type;
cout << "Enter initial amount to deposit: ";
cin >> deposit;
cout << "\nAccount created....\n";
}
void account::dep(int x) {
deposit += x;
}
void account::show_account() {
cout << "\nAccount no: " << accno;
cout << "\nHolder name: " << name;
cout << "\nbalance: " << deposit;
}
int account::repacno() {
return accno;
}
void write_account();
void display_sp(int);
void display_all();
int main() {
int choice{};
do {
cout << "\n\n MAIN MENU\n";
cout << "\n1) create account";
/*cout<<"\n2) deposit\n";*/
cout << "\n2) Enquiry";
cout << "\n3) Show all accounts";
cout << "\n4) EXIT>>";
cout << "\nEnter options: ";
cin >> choice;
switch (choice) {
case 1:
write_account();
break;
/*case 2: cout<<"\nEnter acc. no.\n";
cin>>num;
deposit_money(num );
break;
*/
case 2:
{
int num{};
cout << "\nEnter acc. no: ";
cin >> num;
display_sp(num);
}
break;
case 3:
display_all();
break;
case 4:
cout << "\nthanks\n";
break;
default:
cout << "\nInvalid option\n";
break;
}
} while (choice != 4);
}
//****************
void display_all() {
ifstream file("account.dat", ios::binary);
if (!file)
cout << "File could not be opened\n";
elsefor (account ac; file.read((char*)&ac, sizeof(account)); ) {
ac.show_account();
cout << '\n';
}
}
//****************
void write_account() {
ofstream file("account.dat", ios::binary | ios::app);
if (!file)
cout << "Cannot open account file\n";
else {
account ac;
ac.create_account();
if (!file.write((char*)&ac, sizeof(account)))
cout << "Error occurred at writing time!\n";
}
}
//***************
void display_sp(int n) {
ifstream file("account.dat", ios::binary);
if (!file) {
cout << "File could not be opened\n";
return;
}
bool flag{};
for (account ac; !flag && file.read((char*)&ac, sizeof(account)); )
if (ac.repacno() == n) {
cout << "\nBALANCE DETAILS\n";
ac.show_account();
flag = true;
break;
}
if (!flag)
cout << "Account number does not exist\n";
}
MAIN MENU
1) create account
2) Enquiry
3) Show all accounts
4) EXIT>>
Enter options: 1
Enter Account No: 123
Account Holder Name: qwe oiu
Choose type of account: CURRENT/SAVING (C/S): c
Enter initial amount to deposit: 100
Account created....
MAIN MENU
1) create account
2) Enquiry
3) Show all accounts
4) EXIT>>
Enter options: 1
Enter Account No: 234
Account Holder Name: poi uyt
Choose type of account: CURRENT/SAVING (C/S): s
Enter initial amount to deposit: 200
Account created....
MAIN MENU
1) create account
2) Enquiry
3) Show all accounts
4) EXIT>>
Enter options: 3
Account no: 123
Holder name: qwe oiu
balance: 100
Account no: 234
Holder name: poi uyt
balance: 200
MAIN MENU
1) create account
2) Enquiry
3) Show all accounts
4) EXIT>>
Enter options: 2
Enter acc. no: 123
BALANCE DETAILS
Account no: 123
Holder name: qwe oiu
balance: 100
MAIN MENU
1) create account
2) Enquiry
3) Show all accounts
4) EXIT>>
Enter options: 345
Invalid option
MAIN MENU
1) create account
2) Enquiry
3) Show all accounts
4) EXIT>>
Enter options: 2
Enter acc. no: 345
Account number does not exist
MAIN MENU
1) create account
2) Enquiry
3) Show all accounts
4) EXIT>>
Enter options: 4
thanks