Ifstream array help

I'm trying to make a 3 by 3 array from information in a .txt file. Inside the .txt file is
0 0 1
0 1 0
1 0 0
I just dont know how to set my integer num as a proper value from the file. I put stars around that below. Thank you.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
        int arrayfield[3][3];
	int num;

	fstream openfile("txt.txt");//txt.txt is my file
	
	if (openfile.is_open())
	{
		while(!openfile.eof())
		{
			for (int i = 1; i <= 2; i++)
			{
                                for (int f = 1; f<=2;f++ )
				{
				*******	infile >> num;******
					arrayfield[i][f] = num;
				}
			}
			
		}
	}
	else
	{
		cout << endl << "Error" << endl;
	}
Last edited on
Your << is going the wrong way. It should be infile >> num; (just like cin is cin >> num, for example). It's putting input into the variable, so the arrows go into the variable. Also, your outer for loop will only iterate twice; i needs to be initialized to 0, not 1. As a sidenote, you don't really need num at all; you can just put the numbers straight into the array.
Topic archived. No new replies allowed.