I need to write this code to take customer info until they select to stop, then display all the records. My problem is thta I am only displaying the last record. How do I make it print all the records?
Code:
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
class Account
{
private:
string name,
address,
cityStateZip,
telephone_Number,
date_Last_Payment;
double account_Balance;
ofstream File;
public:
Account()
{
name="";
address="";
cityStateZip="";
telephone_Number;
date_Last_Payment;
account_Balance;
}
Account(string N,string A,string C,string T,string D, double Ab)
{ name=N;
address=A;
cityStateZip=C;
telephone_Number=T;
date_Last_Payment=D;
account_Balance=Ab;
}
bool Write();
bool Read();
void Copy(Account & A)
{ this ->name=A.name;
this ->address=A.address;
this ->cityStateZip=A.cityStateZip;
this ->telephone_Number=A.telephone_Number;
this ->date_Last_Payment=A.date_Last_Payment;
this ->account_Balance=A.account_Balance;}
void Cin()
{
char ans;
do{
cout << "Enter Name: " << endl;
getline(cin,name);
fflush(stdin);
cout << "Enter adress: " << endl;
getline(cin,address);
fflush(stdin);
cout << "Enter City/State/zip: " << endl;
getline(cin,cityStateZip);
fflush(stdin);
cout << "Enter telephone number: " << endl;
getline(cin,telephone_Number);
fflush(stdin);
cout << "Enter date of last payment: " << endl;
getline(cin,date_Last_Payment);
fflush(stdin);
cout << "Enter account balance: " << endl;
cin >> account_Balance;
cout << "Do you have another account? (Y|N)" << endl;
cin >> ans;
cin.ignore();
}while (ans == 'y' || ans == 'Y');
}
void Cout()
{
Account A;
fstream File;
char again;
while (!File.eof())
{
cout << "Name: ";
cout << name << endl;
cout << "Address: ";
cout << address << endl;
cout << "City/State/Zip: " << endl;
cout << cityStateZip << endl;
cout << "Telephone number: ";
cout << telephone_Number << endl;
cout << "Last Payment: " << endl;
cout << date_Last_Payment << endl;
cout << "Account Balance: " << endl;
cout << account_Balance << endl;
cout << "\nPress the Enter key to see the next record.\n";
cin.get(again);
}
cout << "That's all the data in the file!\n";
File.close();
}
};
int main()
{
fstream File;
char ans;
Account A;
A.Cin();
A.Write();
A.Read();
A.Cout();
fflush(stdin);
system("pause");
return 0;
}
bool Account::Write ()
{
File.open ("Account.txt",ios::app);
char Tab='\t';
if(!File)
{ cout << " can't open output file"<<endl; return false;}
else File<< name << Tab
<<address<<Tab
<<cityStateZip<<Tab
<<telephone_Number<<Tab
<<date_Last_Payment<<Tab
<< account_Balance<<endl;
return true;
}
bool Account::Read()
{
fstream File;
char Tab='\t';
Account A;
File.read(reinterpret_cast<char *>(&A),sizeof(A));
File.close();
return false;
}