File to a 2D array

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#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];
}

Last edited on
Check to make sure your file is being opened properly. If it's not, it means your file isn't in the correct directory.

1
2
3
4
5
file.open(foodFile)
if (!file)
{
    cout << "Error opening file\n";
}


You can determine the current directory by doing system("cd"); on Windows or system("pwd"); on *nix.

Did you mean to not delimit the itemAmount by a space ' '?
Last edited on
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.
Last edited on
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.
Last edited on
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:
1
2
3
4
while (getline(file, farm, ','))
{
    // ...
}
This can be simplified. Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

int main() {
	constexpr size_t MaxFood {28};
	const std::string foodFile {"ASSGN6-B.txt"};
	std::ifstream file(foodFile);

	if (!file)
		return (std::cout << "Cannot open file\n"), 1;

	std::string ordersArray[MaxFood][3];

	size_t cnt {};

	for (std::string farm; cnt < MaxFood && getline(file, farm, ','); ++cnt)
		file >> ordersArray[cnt][0] >> ordersArray[cnt][1] >> ordersArray[cnt][2] >> ordersArray[cnt][2] >> std::ws;

	for (size_t i {}; i < cnt; ++i)
		std::cout << ordersArray[i][0] << "  " << ordersArray[i][1] << "  " << ordersArray[i][2] << '\n';
}



43900  tomatoes  29413
34910  cassavas  34560.9
117  coconuts  63.18

Topic archived. No new replies allowed.