I have a problem where i need to take different parts from a file and put it into a [28][3] array. the file given has 28 rows and 5 columns, but i do not need two of them in the code. However, none of the values that are being read from the file are being saved to any array cell and I can not figure out why.
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using std::cout;
using std::cin;
using std::ifstream;
using std::string;
using std::getline;
using std::stod;
int main()
{
string ordersArray[28][3];
string foodFile = "ASSGN6-B.txt";
ifstream file(foodFile);
string item;
string farm;
string price;
string itemAmount;
string total;
int i = 0;
file.open(foodFile);
while (!file.eof())
{
getline(file, farm, ',');
//no need for farm name, so it isn't stored
getline(file, itemAmount);
ordersArray[i][0] = itemAmount;
//stores item amount value as double in the array
getline(file, item, ' ');
ordersArray[i][1] = item;
//stores the item name as a string in the array
getline(file, price, ' ');
//no need for individual item price, so it isn't stored in the array
getline(file, total, '\n');
ordersArray[i][2] = total;
//stores the total price collected from that item as a double in the array
i++;
}
cout << ordersArray[0][0];
}
Okay i fixed the file issue. And what is the correct way to use a space as a delimiter? the file given to us has a comma for the first item, and the rest are by spaces, and we cant change it cause they use that file to test our code. When i take it out completely, it atleast stores something into itemAmount, and when i leave it in, and do c << out itemAmount to test, i get nothing.
Have you learned what structs or classes are, yet? You might find using a struct more intuitive than a 2D array.
And what is the correct way to use a space as a delimiter?
I didn't say you were using a space as a delimiter wrong. I was saying you are using a newline as the delimiter for 'itemAmount'. I can't see your file since you haven't provided an example of it. If you show a few lines from the file, it might be more clear what you're doing wrong.
Also, please edit your original post (see the bottom right of the post), and add "[code]" and "[/code]" around your program to format it.
Thanks for the formatting tip. Here are a few file lines
Collins Farm, 43900 tomatoes 0.67 29413
Bart Smith Farms, 34910 cassavas 0.99 34560.9
Allen Farms, 117 coconuts 0.54 63.18
We have not yet learned about structs or classes yet.
One issue is that you are opening the file twice. Your constructor on line 17 is already opening file the file, so remove line 27.
Second, assuming you haven't already done so, change line 33 to getline(file, itemAmount, ' ');
You can check your logic by temporarily printing stuff out in the loop, for example cout << ordersArray[i][0] << ' ' << ordersArray[i][1] << ' ' << ordersArray[i][2] << '\n';
I would also add a safety check within your loop so that it isn't possible to out of bounds:
1 2
if (i >= 28)
break;
You also ideally shouldn't be looping on eof, but it's fine for now. Consider moving line 30 to be your while loop condition: