c++ file input/output...
i'm having a little trouble with file output in c++ just wondering can you guys help me along. the program itself is fairly trivial.
i set up two bank_account objects in main()
acc1 and acc2. i initialise them with values for account#, sortcode, pin, balance, overdraft_limit.
the write_data function is part of the bank_account class.
here it is...
now the write_data function works fine with no problems but it only writes the last object into the file "text.txt".
so test.txt looks like this
whereas i'd like it to be this...
here is my overloaded << operator for a bank_account object...
can anyone advise me as to where i'm going wrong?
i'm having a little trouble with file output in c++ just wondering can you guys help me along. the program itself is fairly trivial.
i set up two bank_account objects in main()
acc1 and acc2. i initialise them with values for account#, sortcode, pin, balance, overdraft_limit.
Code:
main()
{
bank_account acc1(12345, 901047, 6472, 100.25, 10000) ;
bank_account acc2(123, 901, 1234, 150.25, 100) ;
acc1.write_data() ;
acc2.write_data() ;
}
the write_data function is part of the bank_account class.
here it is...
Code:
void bank_account::write_data()
{
ofstream out ;
out.open( "test.txt" ) ;
out << *this ;
out << endl ;
out.close() ;
}
now the write_data function works fine with no problems but it only writes the last object into the file "text.txt".
so test.txt looks like this
Code:
123 901 1234 150.25 100
whereas i'd like it to be this...
Code:
12345 901047 6472 100.25 10000
123 901 1234 150.25 100
here is my overloaded << operator for a bank_account object...
Code:
ostream& operator<<(ostream& os, const bank_account& acc )
{
long acc_num;
long sort ;
int pin ;
double bal ;
double overdraft ;
acc.get_details(acc_num, sort, pin, bal, overdraft) ;
os << acc_num << " " << sort << " " << pin << " " << bal << " " << overdraft << " " << endl ;
return os ;
}
can anyone advise me as to where i'm going wrong?
Last edited: