Outputting multiple lines in file.

write_account() should input those lines in "account.dat" file when accessing create_account() in class account.
I want to simply input multiple arguments of create_account() in "account.dat" file and display them on screen using display_sp() and show_account().
file.write() is it wrong? want to do this in binary, if possible?

I'm stuck at (file.good) error.
Please, ignore extra case of deposit.
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();
  }
Last edited on
L101. You can't test if an error occurred after you have closed the file! Once the file is closed then the file is always not good!

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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cctype>

using namespace std;

constexpr size_t maxNam {50};

class account {
	int accno {};
	char name[maxNam] {};
	int deposit {};
	char type {};

public:
	void create_account();
	int repacno() const;
	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, maxNam);

	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. " << accno;
	cout << "\nHolder name: " << name;
	cout << "\nbalance:\n" << deposit;
}

int account::repacno() const {
	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("account.dat", ios::binary | ios::out | ios::app);

	ac.create_account();
	file.write((char*)&ac, (sizeof(account)));

	if (!file)
		cout << "Error occurred at writing time!\n";
}

//***************
void display_sp(int n) {
	account ac;
	bool flag {};
	fstream file ("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();
}

Last edited on
Topic archived. No new replies allowed.