Multiple lines outputting in files
Nov 27, 2021 at 9:39am Nov 27, 2021 at 9:39am UTC
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
#include <iostream>
#include<fstream>
#include<iomanip>
#include<cctype>
using namespace 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.\n" ;
cin>>accno;
cout<<"\nAccount Holder Name: \n" ;
cin.ignore();
cin.getline(name, 50);
cout<<"\nChoose type of account: CURRENT/SAVING (C/S) :\n" ;
cin>>type;
cout<<"\nEnter initial amount to deposit\n" ;
cin>>deposit;
cout<<"\nAccount created....\n" ;
}
void account::dep(int x){
deposit+=x;
}
void account::show_account(){
cout<<"\nAccount no.\n" <<accno;
cout<<"\nHolder name:\n" ;
cout<<name;
cout<<"\nbalance:\n" <<deposit;
}
int account::repacno(){
return accno;
}
void write_account();
void display_sp(int );
int main()
{
int choice;
int num;
do {
cout<<"\n\n MAIN MENU\n" ;
cout<<"\n1) create account\n" ;
/*cout<<"\n2) deposit\n";*/
cout<<"\n2) Enquiry\n" ;
cout<<"\n3) EXIT>>\n" ;
cin>>choice;
switch (choice)
{
case 1: write_account();
break ;
/*case 2: cout<<"\nEnter acc. no.\n";
cin>>num;
deposit_money(num );
break;*/
case 2: cout<<"\nEnter acc. no.\n" ;
cin>>num;
display_sp(num);
break ;
case 3: cout<<"\nthanks\n" ;
break ;
default : cout<<"\ndefault\n" ;
break ;
}
cin.ignore();
cin.get();
}while (choice!=3);
return 0;
}
//****************
void write_account()
{
account ac;
ofstream file;
file.open("account.dat" , ios::binary| ios::out| ios::app);
ac.create_account();
file.write((char *) &ac, (sizeof (account)));
if (!file.good()) {
cout << "Error occurred at writing time!" << endl;
}
}
//***************
void display_sp(int n){
account ac;
bool flag=false ;
fstream file;
file.open("account.dat" ,ios::binary| ios::in);
if (!file)
{
cout<<"File could not be open !! Press any Key..." ;
return ;
}
cout<<"\nBALANCE DETAILS\n" ;
while (!flag && file.read((char *)&ac, sizeof (account)))
if (ac.repacno() == n) {
ac.show_account();
flag = true ;
}
if (!flag)
cout << "\n\nAccount number does not exist" ;
getch();
}
https://www.theengineeringprojects.com/2019/11/introduction-to-access-modifiers-in-c.html
Last edited on Dec 26, 2021 at 9:30am Dec 26, 2021 at 9:30am UTC
Nov 27, 2021 at 12:07pm Nov 27, 2021 at 12:07pm UTC
Perhaps:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
#include <iostream>
#include <fstream>
using namespace 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" ;
else
for (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
Topic archived. No new replies allowed.