So right now I have a tab-delimited file called "customers.txt" where each customer database entry has the following format:
id<tab>name<tab>address<tab>city<tab>postcode<tab>purchaseDate<tab>amountowed
A few sample entries looks like this:
C0001 George I. Bullock 1295 Donec St. Limena 9545 14/07/13 3548.17
C0002 Kyla M. Byrd Ap #505-6018 Adipiscing Ave Camporotondo di Fiastrone 5773 05/06/14 6934.61
C0003 Moses J. Decker 533-2825 Turpis Rd. Carnoustie 4473 27/01/14 6801.02
I want to overload the >> operator in my Customer class file so that it correctly reads all the relevant fields, instantiates a Customer object from those fields, and then stores the newly-created Customer object inside a Customer vector. But I have no idea how to go about it. This is what I've done so far:
//main.cpp:
vector <Customer> cList;
ifstream cDbase("datafiles/customers.txt");
if (!cDbase)
{
cerr << "Customers.txt could not be read." << endl;
exit(1);
}
if (cDbase.good())
{
while (!cDbase.eof())
{
/*
I HAVE NO IDEA WHAT TO PUT HERE
*/
Customer newCust = Customer(in);
cList.push_back(newCust);
}
cDbase.close();
}
.
//Customer.h
private:
std::string custID;
std::string custName;
std::string custAddress;
std::string custCity;
int custPCode;
Date purchaseDate;
double amountOwed;
public:
friend std::istream& operator>> (std::istream &in, Customer &cust);
.
//Customer.cpp:
istream& operator>> (istream &in, Customer &cust)
{
in >> cust.custID;
in >> cust.custName;
in >> cust.custAddress;
in >> cust.custCity;
in >> cust.custPCode;
in >> cust.amountOwed;
return in;
}
Like I said, I have no idea what I'm doing, so any help would be much appreciated.
Another issue is the Date field. How do I read each part of the Date listed in the text file and convert it into a form where I can call my Date constructor? For example:
convert:
14/07/13
to:
Date(14,7,13)