The problem is with this line
inCustomerDetails.getCustomerProduct(productName, productID, unitCost, quantityPurchased, x);
data.txt
5
Angie Ang S1234567 Y 4
Choco P0001 5.50 2
Rice P0002 10.00 1
Bread P0003 2.00 1
Chicken P0004 7.50 1
program code
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
class Product
{
private:
string productName;
string productID;
double unitCost;
int quantityPurchased;
public:
Product() ;
void setProduct(const string, const string, const double, const int);
void getProduct(string&, string&, double&, int&);
};
Product:roduct()
{
productName = "";
productID = "";
unitCost = 0.0;
quantityPurchased = 0;
}
void Product::setProduct(const string inProductName, const string inProductID, const double inUnitCost, const int inQuantityPurchased)
{
productName = inProductName;
productID = inProductID;
unitCost = inUnitCost;
quantityPurchased = inQuantityPurchased;
}
void Product::getProduct(string &outProductName, string &outProductID, double &outUnitCost, int &outQuantityPurchased)
{
outProductName = productName;
outProductID = productID;
outUnitCost = unitCost;
outQuantityPurchased = quantityPurchased;
}
class Customer: public Product
{
private:
string custName;
string custID;
char preStatus;
int numOfProduct;
Product *totalProduct;
public:
Customer() ;
void setCustomer(const string, const string, const char, const int);
void getCustomer(string&, string&, char&, int&);
void setCustomerProduct(const Product[]);
void getCustomerProduct(string&, string&, double&, int&, const int);
};
Customer::Customer()
{
custName = "";
custID = "";
preStatus = ' ';
numOfProduct = 0;
}
void Customer :: setCustomer(const string inCustName, const string inCustID, const char inPreStatus, const int inNumOfProduct)
{
custName = inCustName;
custID = inCustID;
preStatus = inPreStatus;
numOfProduct = inNumOfProduct;
}
void Customer::getCustomer(string &outCustName, string &outCustID, char &outPreStatus, int &outNumOfProduct)
{
outCustName = custName;
outCustID = custID;
outPreStatus = preStatus;
outNumOfProduct = numOfProduct;
}
void Customer::setCustomerProduct(const Product inProduct[])
{
totalProduct = new Product[numOfProduct];
for(int i = 0 ; i < numOfProduct; i++)
{
totalProduct[i] = inProduct[i];
}
}
void Customer::getCustomerProduct(string &outProductName, string &outProductID, double &outUnitCost, int &outQuantityPurchased, const int index)
{
totalProduct[index].getProduct(outProductName, outProductID, outUnitCost, outQuantityPurchased);
}
bool getData(Customer[], const int, int&);
void writeFile(Customer[], const int);
int main()
{
int totalTrans;
const int customerSize = 50;
Customer customerDetails[customerSize];
//Get data function to read data from data.txt file
if(getData(customerDetails, customerSize, totalTrans) == true)
{
writeFile(customerDetails, totalTrans);
}
ifstream creport;
string cbuf;
creport.open("customer.txt");
if (!creport.good())
{
cout << "File not found\n";
return 1;
}
while(!creport.eof())
{
getline(creport, cbuf);
cout << cbuf << endl;
}
system("pause");
return 0;
}
bool getData(Customer inCustomerDetails[], const int maxSize, int &outTotalTrans)
{
ifstream fin;
Product *tempProducts;
int switchIndex = 0;
string firstName, lastName, customerID;
char preniumStatus;
int numOfProduct = 0;
string productName, productID;
double unitCost = 0.0;
int quantityPurchased = 0;
int customerIndex = 0, productIndex;
bool getFlag = true;
bool readFlag = true;
fin.open("data.txt");
if (!fin.good())
{
cerr << "Error : File not found\n";
}
while(!fin.eof() && readFlag == true)
{
switch(switchIndex)
{
case 0:
fin >> outTotalTrans;
if(outTotalTrans < maxSize)
{
switchIndex = 1;
}
else
{
cout << "System error : Total customer exceed size 50" << endl;
getFlag = false;
readFlag = false;
fin.close();
break;
}
break;
case 1:
productIndex = 0;
if(customerIndex < outTotalTrans)
{
fin >> firstName >> lastName >> customerID >> preniumStatus >> numOfProduct;
string fullName = firstName + " " + lastName;
inCustomerDetails[customerIndex].setCustomer(fullName, customerID, preniumStatus, numOfProduct);
tempProducts = new Product[numOfProduct];
customerIndex++;
switchIndex = 2;
}
else
{
readFlag = false;
fin.close();
break;
}
break;
case 2:
if(productIndex < numOfProduct)
{
fin >> productName >> productID >> unitCost >> quantityPurchased;
tempProducts[productIndex].setProduct(productName, productID, unitCost, quantityPurchased);
productIndex++;
}
else
{
delete [] tempProducts;
productIndex = 0;
switchIndex = 1;
}
break;
default:
break;
}
}
fin.close();
return getFlag;
}
void writeFile(Customer inCustomerDetails[], const int inTotalTrans)
{
ofstream cfile;
ofstream sfile;
string name, custID;
char preStatus;
int numOfProduct = 0;
string productName, productID;
double unitCost = 0.0;
int quantityPurchased = 0;
cfile.open("customer.txt");
//sfile.open("sales.txt");
for(int i = 0; i < inTotalTrans; i++)
{
double totalSales = 0.0;
double totalPoints = 0.0;
inCustomerDetails[i].getCustomer(name, custID, preStatus, numOfProduct);
cfile << "Customer Name: "<< name << "\n";
cfile << "Customer ID: "<< custID << "\n";
cfile << "Premium Customer: "<< preStatus << "\n";
for(int x = 0; x < numOfProduct; x++)
{
inCustomerDetails[i].getCustomerProduct(productName, productID, unitCost, quantityPurchased, x);
//cfile << productID << "\t" << productName << "\t" << quantityPurchased << "\n";
totalSales += unitCost;
totalPoints = totalSales * 100;
}
cfile << "Total Sales: " << totalSales << "\n";
if(preStatus == 'N' || preStatus == 'n')
cfile << "Reward Points: " << "NA" << "\n\n";
else
cfile << "Reward Points: " << totalPoints << "\n\n";
cfile << "++++++++++++++++++++++++++++++++++++++++++++\n";
}
}