question about classes

Hi,
i have to create a bankAccount class, witch describes a Bank Account. the public members are: the name of the Bank, the name of a Client, account number and money.
I have to foresee the constructor and a print function in this class.
In the file "account.txt" is written data of several account numbers.
In the programm i have to read this information one after another and print all such data, where money is greather then 500 Dollars.

my question is, how should write a code to read this information from this file.

Here is my code, but it's not working.
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class bankAccount
{
public:
	string Bname, Cname;
	int Anumber, money;
	bankAccount (string B, string C, int A, int M) :
	Bname(B), Cname(C), Anumber(A), money(M) {}
	void print()
	{
		cout<<"The name of the Bank is "<<Bname<<' '<<"Client's name is "
			<<Cname<<' '<<"Account number is "<<Anumber<<' '
			<<"The money on theis Account is "<<money<<" Dollar "<<endl;
	}
};
int main()
{
	string b, c;
	int a, m;
	ifstream fin("account.txt");
	while(fin>>b>>c>>a>>m)
	{
		bankAccount x(b, c, a, m);
	if(x.money > 500 )
		x.print();
	}
	return 0;
}

sorry about my English.
¿what do you mean with "it's not working"?
closed account (DSLq5Di1)
What does your input look like? multi-word names would cause a problem.
as i know, in classes we have to exactly know how many data in file is(isn't it?), in order to put them one by one . and in this case i don't know...so...do you have a guess how to do this?
sloppy9 in this case i know that multi-word name is not the problem...
IMO the problem is in the main function
1
2
3
4
5
6
while(fin>>b>>c>>a>>m)
	{
		bankAccount x(b, c, a, m);
	if(x.money > 500 )
		x.print();
	}
Hi

First you should decide about the data structure which are stored in the file, I mean decide about the format. Only if you know the format of the data store in a file, you are than able to write some code reading the data from the file.

so ,

First define the format of data

Second write code to check whether the format of data stored in the target file are alright,

Third write some other code to read data.


You may combine the 2end and third steps just in one step

Hope it helps

by the way

before you read data from a file you need to check whether it is opened, and use eof to check whether there are still data to be read. Finally when you are finished with reading close the file close

for example

1
2
3
4
5
6
7
8
9
ifstream ifs;
	ifs.open( fileName , ifstream::in ) ;

	if(ifs.is_open() ){
		while(!ifs.eof()){
			//do some reading
		}
		ifs.close();
	}

Last edited on
yes i know the data structure.

instead this:
1
2
3
4
5
6
while(fin>>b>>c>>a>>m)
	{
		bankAccount x(b, c, a, m);
	if(x.money > 500 )
		x.print();
	}


i wrote this, as you said.
1
2
3
4
5
6
7
8
9
10
ifstream fin("account.txt");
	if(fin.is_open() )
	{
		while(!fin.eof() )
		{
			bankAccount x(b, c, a, m);
			x.print();
		}
		fin.close();
	}

but still not working:\\
therockon7throw wrote:
before you read data from a file you need to check whether it is opened, and use eof to check whether there are still data to be read. Finally when you are finished with reading close the file close

This is a bad advice. Checking against eof() often make one loop iteration too much and if the file is not correct you end up in an infinite loop. What lena already had was much better because it stops both at the end of the file and if a read fails. Checking is_open() is not really needed unless you want to print an error message or something. The stream is automatically closed in the destructor so calling close() here is not really necessary.

lena123, can you show the content of account.txt?
Last edited on
@lena123

Please as Peter asked tell us about the data format being in the file, the data do not need to be real, but just provide us the format with some dummy datas, so we can help
this is just my homework :D so It doesn't matter, i can write it myself.For Example:

Bank name: Client name: Account number: money$$:

TBC David 5559898995 1000
GermanBank Hanna 4545415151 150
Europabank lena 1545457875 600000

and ect.
Last edited on
The problem is that some of the account numbers are too large to be stored in an int. Try using a larger type like long long.
Peter, thank you very much!!! it works now:))
Topic archived. No new replies allowed.