0

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 by Reverend Jim: Fixed formatting

3
Contributors
7
Replies
8
Views
9 Years
Discussion Span
Last Post by VernonDozier
0

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.

0

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.

0

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.

0

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.

0

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.

0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.