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.
#include <iostream>
#include <string>
#include <fstream>
usingnamespace 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;
}
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
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
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.
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