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";
}
}

The problem is with this line:

What is the problem with that line?

After 27 posts you should know that you should use CODE-tags when posting code.
So read this about code-tags and problem-description.

But since I'm in a good mood, I've thrown your code in my compiler and the only thing it comes up with is line 27: Product:roduct() should be: Product::Product() Compiles fine when changed

It has an error of -1073741819 (0xC0000005) for line 277

What is the problem with that line?

After 27 posts you should know that you should use CODE-tags when posting code.
So read this about code-tags and problem-description.

But since I'm in a good mood, I've thrown your code in my compiler and the only thing it comes up with is line 27: Product:roduct() should be: Product::Product() Compiles fine when changed

The totalProduct pointer probably is not what you expect it to be inside the void Customer::getCustomerProduct(...) .

I.e. you have not allocated any Products and are trying to call getProduct() on a non-existing object. totalProduct is left uninitialized at this point <-> you should initialize it to zero in the Customer's constructor and make sure that it later on will point to allocated Product(s) before you call getCustomerProduct().

Regarding the readability, you really should learn to indent your code.

Can show me how to modify my code?

The totalProduct pointer probably is not what you expect it to be inside the void Customer::getCustomerProduct(...) .

I.e. you have not allocated any Products and are trying to call getProduct() on a non-existing object. totalProduct is left uninitialized at this point <-> you should initialize it to zero in the Customer's constructor and make sure that it later on will point to allocated Product(s) before you call getCustomerProduct().

Regarding the readability, you really should learn to indent your code.

To get you started, first of all, add: #include <cassert> in your program, Then in the Customer's constructor add:

Customer::Customer()
{
    ... 
    totalProduct = 0; // Initialize the pointer to zero
}

Then in the failing function add:

void Customer::getCustomerProduct(...)
{
    assert(totalProduct != 0);    // totalProduct must NOT be 0 at this point 
    // rest of code follows ...
}

Now, the rest is up to you to go through the program flow and fix it so that the error condition does not occur.

i'm still having the same error

Did you follow mitrmkar's advice?

Now, the rest is up to you to go through the program flow and fix it so that the error condition does not occur.

If yes: post your newest code

This article has been dead for over six months. Start a new discussion instead.