1

I would like to store the below txt file into a few arrays using C++ but I could not do it as the white space for the item name will affect the storing.

Monday //store in P1 string
14-February-2022 //store in P2 string
Red Chilli //store in P3 array, problem occurs here as i want this whole line to be store in the array 
A222562Q //store in P4 array
1.30 2.00 //store in P5 and P6 array
Japanese Sweet Potatoes //repeat storing for Item 2 until end of the file
B807729E
4.99 1.80
Parsley
G600342k
15.00 1.20

Below is the coding I have tried to do to store the data in the array. It might not be the best way. Can provide me a better ways to store the data. Thanks

ifstream infile;
infile.open("itemList.txt");
infile >> P1;
infile >> P2;

for (int i = 0; i < sizeof(P3); i++) {
    
    infile >> P3[i];
    infile >> P4[i];
    infile >> P5[i];
    infile >> P6[i];

}
Andy
  • 25
  • 5
  • Does the loop iterate more often than you expect? – Yunnosch Apr 12 '22 at 08:52
  • the loop works well just that the "Red Chilli" makes the system separate it into two arrays which I want it to store in only one array. I think is because of the white spacing which causes this problem. So I would like to know is there any solution I can solve that – Andy Apr 12 '22 at 09:09
  • What do you think `sizeof(P3)` is? Welcome to Stack Overflow. Please read the [About](http://stackoverflow.com/tour) page soon and also visit the links describing [How to Ask a Question](http://stackoverflow.com/questions/how-to-ask) and [How to create a Minimal Reproducable Example](https://stackoverflow.com/help/minimal-reproducible-example). Providing the necessary details, including your MRE, compiler warnings and associated errors, and sample data if any, will allow everyone here to help you with your question. – David C. Rankin Apr 15 '22 at 05:54
  • @Andy would be good to hear if you found a solution? – codeling Apr 15 '22 at 21:21

1 Answers1

0

You can use std::getline instead of the operator>> for the P3 field:

constexpr int NumOfEntries = 3;
std::array<std::string, NumOfEntries> P3;
// ... and assuming P4, P5, P6 are declared similarly above
for (int i = 0; i < NumOfEntries; i++) {
    
    std::getline(infile, P3[i]);
    infile >> P4[i];
    infile >> P5[i];
    infile >> P6[i];
    infile.ignore();
}

You don't show the definitions of P3 to P6, and the i < sizeof(P3) condition in your for loop looks strange - I can't come up with a declaration for P3 for which that loop would then work properly. But apparently you already know before how many entries there are in the file, right? So I use std::vector<std::string> for P3, and a constant NumOfEntries for the loop condition.

cin.ignore() is required to ignore the newline character after P6 input; otherwise the next getline would only deliver an empty string in its next turn. See for example this other question for more details.

codeling
  • 11,056
  • 4
  • 42
  • 71
  • 1
    May be worth inquiring further just what `P3` is in `for (int i = 0; i < sizeof(P3); i++)`... Something seems amiss with the loop definition. – David C. Rankin Apr 15 '22 at 05:52