I have a txt file of this format:

2
Jack        S123    Y   1
Choco   P0001   5.50    2

Jane    S456    N   2
Rice    P0002   10.00   2
Chicken P0004   7.50    1

how do i create the loop to store the data into 2 classes?

my code

for (int i = 0; i < 2; i++)
{
for (int j = 0; j < numOfPurchased; j++)

        fin >> customerName >> customerID >> preniumStatus >> numOfPurchased;
        c[i].setInfo(customerName, customerID, preniumStatus, numOfPurchased);
        getline(fin, space);

            fin >> pname >> pID >> uCost >> qPurchased;
            p[j].setInfo(pname, pID, uCost, qPurchased);
}
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Ed would start with something like this:

fin >> records;

for (int i = 0; i < records; ++i) {
  // Load the customer data
  fin >> name >> id >> status >> nproducts;
  c[i].SetInfo(name, id, status, nproducts);

  // Load the product data for this customer
  for (int j = 0; j < nproducts; ++j) {
    fin >> name >> id >> cost >> nunits;
    p[j].SetInfo(name, id, cost, nunits);
  }
}

That's assuming c and p are both big enough to hold everything and that each customer has an array of products. Ed would more likely use a vector and push_back as needed, and probably also store the product data inside the customer object.

Ed would start with something like this:

fin >> records;

for (int i = 0; i < records; ++i) {
  // Load the customer data
  fin >> name >> id >> status >> nproducts;
  c[i].SetInfo(name, id, status, nproducts);

  // Load the product data for this customer
  for (int j = 0; j < nproducts; ++j) {
    fin >> name >> id >> cost >> nunits;
    p[j].SetInfo(name, id, cost, nunits);
  }
}

That's assuming c and p are both big enough to hold everything and that each customer has an array of products. Ed would more likely use a vector and push_back as needed, and probably also store the product data inside the customer object.

I still got the same error, .exe has stop working when I run it.

You didn't mention anything about an error. I'd guess that you're trying to write to an index in either c or p that's out of bounds, but without more details on the error and more code, Edward can't offer more specific advice.

You didn't mention anything about an error. I'd guess that you're trying to write to an index in either c or p that's out of bounds, but without more details on the error and more code, Edward can't offer more specific advice.

I created c[50] and p[100], is it because of that?

The error wasn't clear either, it juz says myfile.exe has stop working.

Is that all there was to the message? Usually there's more to it, like "Could not write to address 0x******" or some such gobbledygook that seems useless but can help me localize the problem a bit more.

Is that all there was to the message? Usually there's more to it, like "Could not write to address 0x******" or some such gobbledygook that seems useless but can help me localize the problem a bit more.

Process returned -1073741819 (0xC0000005)

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