file to array

im trying to use data from a file and turn it into an array
but im stuck. the output is a just random numbers i need help

the file contain 2 columns and 5 rows
1__2
3__4
5__6
7__8
9__10


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
53
54
55
56
57
58
59
60
61
62
63
64
  
#include<iostream>
  #include<fstream>
  #include<cstdlib>
  #include<string>
  
  using namespace std;

  int main()
  {
	

	//Now begin reading values from the file.
	ifstream inFile;

	string filename;
	
	cout << "Which file would you like to open? Include the file's name and extension: "
		 << endl;
	cin >> filename;
	
	inFile.open(filename.c_str()); //attempt to open file for input
	
	if(!inFile.fail()) //if it doesn't fail, the file exists 
	{
		cout << "A file by the name " << filename << " exists."
			 << endl;
		
	}
	
	int value;
	int SIZE=10;
	int array[10][2];
	int i=0;
	int j=0;
	
	inFile >> value;
	while (inFile.good()) //continue until the end of the file
	{
		 
		
		array[i][j]=value;
		
		for(i=0; i<SIZE; i++)
		{
		  cout<<endl;
		
		  for( j=0; j<SIZE; j++)
		  {
		    cout<<endl;
		  }
		}
		cout <<array[i][j]<<endl;
		 inFile >> value;
	}
		
		
	
	
	inFile.close(); //closes the file
	
	return 0;
  }
  
Don't print the array until you finish reading the file.
so i need to close file before i print the array
It doesn't matter when you close the file.
then why you tell me don't prrnt the array until i finish reading the file doesn't that mean i have to close the file.
When I finish reading a book, it is still open until I close it.

When you finish reading a file, it is still open until you close it. It doesn't matter when you close it, it only matters when you finish reading it.
ok how do i stop it from reading
Loop on the input operation:
83
84
while(inFile >> value)
{
Also, make sure the for loop is not inside the while loop.
im still having issues with the program but im getting close. the problem is its repeating numbers

output
1
1
1
2
2
3
3

and i want in

output
1__2
3__4
5__6

any advise


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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<iostream>
  #include<fstream>
  #include<cstdlib>
  #include<string>
  
  using namespace std;

  int main()
  {
	

	//Now begin reading values from the file.
	ifstream inFile;

	string filename;
	
	cout << "Which file would you like to open? Include the file's name and extension: "
		 << endl;
	cin >> filename;
	
	inFile.open(filename.c_str()); //attempt to open file for input
	
	if(!inFile.fail()) //if it doesn't fail, the file exists 
	{
		cout << "A file by the name " << filename << " exists."
			 << endl;
		
	}
	
	int value;
	int ar[5][2];
	int k=0;
	int j=0;
	
	
	
	if(inFile.good()) //continue until the end of the file
	{
	  
	while(inFile >> value)
	{
		    
			
	  for( j=0; j<2; j++)
		{
		  
		  cout<<endl;
		 
		  for( k=0; k<2; k++)
		  {
		
		      ar[k][j]=value;
		      
		      
		      cout<<ar[k][j]<<endl;
		    
		  }
		}
		
	}
	}
		//cout<<j<<endl;
		//cout <<value<<endl;
		
		
		
		
	
		
		inFile.close(); //closes the file
	
	
	
	
	return 0;
  }
  

I will repeat: the for loop should not be inside the while loop.
ok thanks got it fixed

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76



 #include<iostream>
  #include<fstream>
  #include<cstdlib>
  #include<string>
  
  using namespace std;

  int main()
  {
	

	//Now begin reading values from the file.
	ifstream inFile;

	string filename;
	
	cout << "Which file would you like to open? Include the file's name and extension: "
		 << endl;
	cin >> filename;
	
	inFile.open(filename.c_str()); //attempt to open file for input
	
	if(!inFile.fail()) //if it doesn't fail, the file exists 
	{
		cout << "A file by the name " << filename << " exists."
			 << endl;
		
	}
	
	int value;
	int ar[5][2];
	int k=0;
	int j=0;
	int i=0;
	
	
	while(inFile.good()) //continue until the end of the file
	{
	  
	inFile >> value;
	    
	    for( i=0; i<5; i++)
	    {
	      cout<<endl;
		  for( k=0; k<2; k++)
		  {   
		      ar[i][k]=value;
		      
		      
		      cout<<ar[i][k];
		      
		     inFile >> value;
		    }
	    }
	cout<<endl;
		
	}
	
		//cout<<j<<endl;
		//cout <<value<<endl;
		
		
		
		
	
		
		inFile.close(); //closes the file
	
	
	
	
	return 0;
  }

You made it worse, not better. Your code should look like this:
1
2
3
4
5
6
while(inFile >> value)
{
    //...no for loops at all in here...
}

//...feel free to put for loops here... 
Last edited on
Topic archived. No new replies allowed.