Banking class with file i/o

I have built a banking class, not fully done, but in the beginning stages, but I have come across a problem. I cant read the parts of the file I need and print them out using the class variables.

I get random numbers for my account numbers, 0 for account balance, and a name only shows up for the first entry in the file for account name.

my codes are as follows:

MainBankClass.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef MAINBANKCLASS_H
#define MAINBANKCLASS_H
#include <iostream>

using namespace std;


class Banking 
{
private:
	string	acctName;		// Name on the account
	int acctNumber[13];			// Account number
	float acctBalance;		// Amount in the account
public:
	void getAcctInfo();		// Get name on account for displaying relevant information
	void createNewAccount();	// Create a new account and assign it a random account number
	// void transferFunds();		// Transfer funds checking <--> savings, passing amount as the argument, saving for later day
	void invalid(char *);		// If an invalid option is chosen
	char menu();				// Print the main menu for the user.
	void fatal(char *);
	Banking();
};
#endif 



BankDriver.cpp
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <time.h>
#include "MainBankClass.h"


Banking::Banking()
{
	acctNumber[13] = 0;
	acctBalance = 0;
}

/********************************\
| The following code is to print the menu	|
| and recieve the users choice on what		|
| they want to do with the ATM				|
\********************************/

char Banking::menu()
{
	char choice;
	cout << "\t\t -=[ Main Menu ]=- \n\n"
		   << "\tA) Create New Account\n"
		   << "\tB) View Account Balance\n"
		   << "\tC) Transfer Funds From Checking To Savings\n"
		   << "\tD) Transfer Funds From Savings To Checking\n"
		   << "\tE) Exit\n"
		   << "\n\n\tSelection: ";
	cin >> choice;
	cin.ignore();
	choice = toupper(choice);
	
	while(!isalpha(choice))
	{
		invalid("[!!] Invalid selection.\n[!!] Choose a valid option: ");
		cin >> choice;
		cin.ignore();
	}

	switch(choice)
	{
		case 'A':
			createNewAccount();
			break;
		case 'B':
			getAcctInfo();
			break;
		case 'C':
			cout << "Choice C" << endl;
			break;
		case 'D':
			cout << "choice D" << endl;
			break;
		case 'E':
			cout << "Exiting" << endl;
			exit(1);
			break;
		default:
			system("cls");
			invalid("\n\n\n\n\t\t [!!] Invalid Selection \n\t\t Good-bye \n\n\n\n\n\n\n");
			exit(1);
			break;
	}
	

	return choice;
}


/***************************\
| Incase an invalid decision to made	|
| this throws the error message sent	|
| to it by the calling area				|
\***************************/
void Banking::invalid(char *msg)
{
	cout << msg;
}


/*************************\
| Used if files can not be opened	 |
| and exits with code 251:			 |
| miscommunication with server	 |
\*************************/

void Banking::fatal(char *msg)
{
	cout << msg;
	exit(1);
}


/***************************\
| Create an account, either checking	|
| or savings, or both.					|
| Must should create a randomly		|
| generated account number that will	|
| correspond with each account.		|
\***************************/

/************************\

NOTE::	WILL BE UPDATED
TO CONTAIN A PIN FOR
ACCOUNT VERIFICATION

*************************/
void Banking::createNewAccount()
{
	srand(time(NULL));	// Seed random generator with time initialized to NULL
	char acctChoice;		// choice for the account type
	float initCheckDeposit = 0;		// The initial deposit for checking account
	float initSaveDeposit = 0;		// The initial deposit for saving account
	
	
	ofstream checkFile("checking.dat", ios::out | ios::binary | ios::app);		// For saving checking accounts
	ofstream saveFile("saving.dat", ios::out | ios::binary | ios::app);			// For saving savings accounts
	system("cls");
	cout << "\t\t-=[ New Account Creation ]=-\n\n" << endl;
	cout << "A) Checking Account\n"
		  << "B) Savings Account\n"
		  << "C) Checking and Saving Account\n" << endl;
	cout << "New account type: ";
	cin >> acctChoice;
	acctChoice = toupper(acctChoice);
	cin.clear();
	cin.sync();

	switch(acctChoice)
	{
		case 'A':
			system("cls");
			cout << "\t\t -=[ New Checking Account ]=- \n" << endl;
			cout << "Name of the main holder to be on the account: ";
			getline(cin, acctName);
			cout << "Initial deposit amount: $";
			cin >> initCheckDeposit;
			
			if(!checkFile)
				fatal("[!!] Fatal Error 251: Miscommunication with server\n");
			
			for(int i = 0; i < 12; i++)
			{
				acctNumber[i] = (rand() % 10);		// Build a random checking account number
			}

			checkFile <<  acctName << endl;
			checkFile <<  acctNumber << endl;
			checkFile << initCheckDeposit << endl;

			break;

		case 'B':
			system("cls");
			cout << "\t\t -=[ New Savings Account ]=- \n" << endl;

			cout << "Name of the main holder to be on account: ";
			getline(cin, acctName);
			cout << "Deposit Amount: $";
			cin >> initSaveDeposit;

			if(!saveFile)
				fatal("[!!] Fatal Error 251:  Miscommunication with server\n");

			for(int i = 0; i < 12; i++)
			{
				acctNumber[i] = (rand() % 10);
			}

			saveFile << acctName << endl;
			saveFile << acctNumber << endl;
			saveFile << initSaveDeposit << endl;

			break;

		case 'C':
			system("cls");
			cout << "\t -=[ New Checking / Saving Account ]=- \n" << endl;

			cout << "Name of the main holder to be on account: ";
			getline(cin, acctName);
			cout << "Checking Deposit Amount: $";
			cin >> initCheckDeposit;
			cout << "Saving Deposit Amount: $";
			cin >> initSaveDeposit;

			if(!saveFile || !checkFile)
				fatal("[!!] Fatal Error 251:  Miscommunication with server\n");

			for(int i = 0; i < 12; i++)
			{
				acctNumber[i] = (rand() % 10);
			}

			saveFile << acctName << endl;
			saveFile << acctNumber << endl;
			saveFile << initSaveDeposit << endl;
			
			checkFile << acctName << endl;
			checkFile << acctNumber << endl; 
			checkFile << initCheckDeposit << endl;
			break;

		default:
			system("cls");
			fatal("[!!] Invalid option\n");
			break;

	}
}
			

/*********************\
| Will read in account info	    |
| and display it for the user	    |
\*********************/

void Banking::getAcctInfo()
{
	int i = 0;
	double balance = 0;
	char choice;
	string name;
	fstream checkFile("checking.dat", ios::in | ios::binary | ios::beg);
	fstream saveFile("saving.dat", ios::in | ios::binary | ios::beg);
	system("cls");
	cout << "\t\t -=[ View Account Balance ]=-\n\n";

	cout << "A) View Checking Account\n"
		  << "B) View Saving Account\n"
		  << "C) View Checking \\ Saving Account\n" << endl;
	cout << "Choice: " << endl;
	cin >> choice;
	choice = toupper(choice);
	
	if(!isalpha(choice))
		fatal(" [!!] Invalid Choice");
	
	switch(choice)
	{
		case 'A':
			cin.clear();
			system("cls");
			cout << "\t\t -=[ View Checking Account ]=-\n\n" << endl;
			cout << "Account Name: ";
			cin.sync();
			getline(cin, name);
			getline(checkFile, acctName);
			while(name != acctName && !checkFile.eof())
			{
				i++;
				getline(checkFile, acctName);
			}

			if(name == acctName)
			{
				checkFile.seekg((sizeof(acctName) * i), ios::beg);
				getline(checkFile, acctName);
				checkFile >> acctNumber[13];
				checkFile >> acctBalance;
				
				system("cls");
				cout << "\t\t -=[ Account Overview ]=-\n\n" << endl;
				cout << "Account Name: " << acctName << "\n"
					  << "Account Number: " << acctNumber << "\n"
					  << "Balance: $" << acctBalance << endl;
			}
			else
			{
				fatal("[!!] Invalid Entry\n");
			}
			break;
	}



}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "MainBankClass.h"
using std::cout;



int main()
{
	Banking bank;

	bank.menu();		// Call the banking menu

	return 0;
}


Any help would be greatly appreciated.
NOTE:: createNewAccount() works fine
Last edited on
1
2
3
checkFile.seekg((sizeof(acctName) * i), ios::beg);
getline(checkFile, acctName);
checkFile >> acctNumber[13];


This line seems simply wrong. What do you want to do?


Maikel

EDIT:

I think it is enough to leave seekg and getline and just read the account number.. but..

 
checkFile >> acctNumber[13];


This wont work. Because you only saved the address of the account number, but not the account number itself.

1
2
3
4
// this should be: for (int j=0; j < 13; j++) checkFile << acctNumber[j]; checkFile << endl;
// or copy(acctNumber, acctNumber+13, ofstream_iterator(checkFile,"")); checkFile << endl;
// i prefer the copy variant! ;-)
checkFile <<  acctNumber << endl; 


And you cant just read in an array of integer... You have to do a loop 13 times. Since the numbers are from 0 - 9 you can read in chars, convert them into numbers and have fun.


NOTE: So in fact, in createNewAccount() was the mistake ;-) haha..
Last edited on
Alright, so currently I can write the correct account number to the file, not the address as maikel mentioned I was doing...

But I am still having trouble reading from the file in getAcctInfo(); I get 0 for balance, and no name, and the account number comes up as the largest negative value allowed by an int...

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
void Banking::getAcctInfo()
{
	int i = 0;
	double balance = 0;
	char choice;
	string name;
	fstream checkFile("checking.dat", ios::in | ios::binary | ios::beg);
	fstream saveFile("saving.dat", ios::in | ios::binary | ios::beg);
	system("cls");
	cout << "\t\t -=[ View Account Balance ]=-\n\n";

	cout << "A) View Checking Account\n"
		  << "B) View Saving Account\n"
		  << "C) View Checking \\ Saving Account\n" << endl;
	cout << "Choice: " << endl;
	cin >> choice;
	choice = toupper(choice);
	
	if(!isalpha(choice))
		fatal(" [!!] Invalid Choice");
	
	switch(choice)
	{
		case 'A':
			cin.clear();
			system("cls");
			cout << "\t\t -=[ View Checking Account ]=-\n\n" << endl;
			cout << "Account Name: ";
			cin.sync();
			getline(cin, name);
			getline(checkFile, acctName);
			while(name != acctName && !checkFile.eof())
			{
				i++;
				getline(checkFile, acctName);
			}

			if(name == acctName)
			{
				checkFile.seekg((sizeof(acctName) * i), ios::beg);
				getline(checkFile, acctName);
				system("cls");
				cout << "\t\t -=[ Account Overview ]=-\n\n" << endl;
				cout << "Account Name: " << acctName << "\n";
				cout << "Account Number: ";
				for(int j = 0; j < 13; j++)
				{
					checkFile >> acctNumber[j];
					cout << acctNumber[j];
				}
				cout << endl;
				checkFile >> acctBalance;
				cout << "Balance: $" << acctBalance << endl;
			}
			else
			{
				fatal("[!!] Invalid Entry\n");
			}
			break;
	}



}
Does this work?

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
            while(name != acctName && !checkFile.eof())
            {
                i++;
                getline(checkFile, acctName);
            }

            if(name == acctName)
            {
                system("cls");
                cout << "\t\t -=[ Account Overview ]=-\n\n" << endl;
                cout << "Account Name: " << acctName << "\n";
                cout << "Account Number: ";
                for(int j = 0; j < 13; j++)
                {
                    char input_number; 
                    stringstream converter; // #include <sstream>
                    checkFile.get(input_number);
                    // optional: if (!isdigit(input_number)) cerr << "ERROR...";
                    converter << input_number;
                    converter >> acctNumber[j];
                    cout << acctNumber[j];
                }
                // if balance still a problem, try to ignore rest in the line
                // checkFile.ignore(numeric_limits<streamsize>::max(), '\n');                
                cout << endl;
                checkFile >> acctBalance;
                cout << "Balance: $" << acctBalance << endl;
            }


EDIT:

The problem is, that you try to read 13 times a NUMBER! but in one line is one number. You got it? What you want to read in is a digit 13 times. not the number. Thats why i did this with characters. Normally you have to check if everything was fine. With converter.fail() or .good() or whatever.

You're welcome

Maikel
Last edited on
It definitely does. Thanks much! Really appreciate it Maikel
Topic archived. No new replies allowed.