Need help with ifstream and arrays

My txt file

1 2 3 4 5 Cheese cake
1 3 5 4 6 Spicy nugget
1 3 5 2 5 Beef Burger
1 3 5 1 3 Potato chips

My code

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
	const int row=4;
	const int col=7;
	int numbers[row][col];
	string food[row];
	ifstream in;

	in.open("input.txt");
	
	for(int i=0; i<row; i++)
	{
		for (int j=0; j<col; j++)
		{
			in>>numbers[i][j];
			
			if(j==6)
			{
				in>>food[i];
			}
		}
	}
	cout<<numbers[0][2];
	cout<<food[0];
return 0;
}


I tried to store the numbers into 2d array while at the same time store the food names into 1d array. But I can't get it work. Can somebody tell me how to edit my code in order to get it work?
Last edited on
I can't get it work
is hardly a helpful statement of your problem. Is it failing to compile? Crashing? Some other error? The more you tell us about the problem, the more we can help you.

Your code should test that the file has actually successfully opened. If it hasn't opened, then that would explain a problem at runtime.

Also, you need to revisit the logic of your loop, and think more rigorously about it. At line 21, when j is 6, that means that you've already read the final column, into numbers[i][6]. The subsequent input on line 23 is going to come from the next line. I don't think that's what you want.
Last edited on
The code can be compiled but it does not show my desired output.

When j=6 (6th column), I want the food names to be stored into food[row] array.

When i try to cout the food, it does not show anything
As I said (you did read all my post, right?), you need to think more clearly about your logic.

Oh, and:

j=6 (6th column)

Are you absolutely sure about that?
Last edited on
I am not so sure how to "separate" the int with the string of the txt file.
I am able to do the 2d array of the int numbers. However, I can't make a separate 1d array that store the names of the foods.
OK... start by looking again at the specific question I asked you in my last post, about a specific thing you wrote.

david97 wrote:
j=6 (6th column)

Are you absolutely sure about that?
Last edited on
I think I am not very sure.
Look at line 17 and 19. What assumptions have you made there about how values of j relate to the positions of columns?

Now look at line 21. Are you using the same assumptions as the ones you made in lines 17 and 19?
Wait... is zheung the same person as david97?

If not, why are you answering questions that I'm asking the OP?
No, I am not him LOL.

I thought i can use j as counters to store the values into array.
No, I am not him LOL.

You are doing staggeringly similar problems:
http://www.cplusplus.com/forum/general/225992/#msg1031491
I thought i can use j as counters to store the values into array.

Yes. That's not what I'm asking. It would be nice if you would read what I actually wrote.

Look at line 17. Look at the range of values j will take during the loop.

Look at line 19. Look at what values j will have when you use it to access the elements of your array. What value will j have on the first iteration of the loop? What column will it be looking at on the first iteration of the loop?
Last edited on
Topic archived. No new replies allowed.