Trouble loading array from .txt file in C++
<pre><code>    #include &lt;cstdlib&gt;
    #include &lt;iostream&gt;
    #include &lt;fstream&gt;
    using namespace std;
    const int MAX=100;




//Declare Functions

    void loadFile(istream &amp;input_file, int arr[], int maxLength, int&amp; length);

    //Main Function

    int main()
    {
        ifstream in_file1;
        ifstream in_file2;

        in_file1.open ("input1.txt");
        in_file2.open ("input2.txt"); 


        int array1[0], array2[0];


        int length;
         loadFile (in_file1, array1, MAX, length);





for (int i = 0; i &lt;= length; i++)
{
    cout &lt;&lt; array1[i] &lt;&lt; endl;
}

cout&lt;&lt;endl&lt;&lt;endl;

loadFile (in_file2, array2, MAX, length);

for (int i = 0; i &lt;= length; i++)
{
    cout &lt;&lt; array2[i] &lt;&lt; endl;
}

        return 0;
        }


    //Function Definitions

    void loadFile(istream &amp;input_file, int arr[], int maxLength, int&amp; length)
{
    int i;
    int input;


    {

        for (i=0; i&lt;maxLength; i++)
        {
            if (!input_file.eof())
            {
            input_file&gt;&gt;input;
            arr[i]=input;
            length=i;
            }
        }
    }

}
</code></pre>

<p>Here is my new code. It still prints a bunch of numbers after the array that aren't in the file. Am I close here? what is still wrong.</p>

<p>Output:</p>

<pre><code>-26
128
184
-4
-51
129
-93
199
115
-92
16
0
-64
56
5
112
-20
160
-56
148
94
18
145
155
178
83
-57
103
-68
69
-53
80
148
131
-82
1
102
-50
192
27
32
-63
34
150
160
160


137
-53
119
-64
-80
186
144
-78
-1
62
-72
86
127
40
53
-93
41
45
194
-19
118
53
31
-52
-27
150
-31
86
-29
34
103
4
-92
90
18
13
49
-57
-51
-7
78
62
97
15
122
154
172
9
-5
108
-33
-33
-60
88
89
190
171
109
39
111
-55
-11
160
16
68
172
168
-64
-14
-15
142
-16
-16
</code></pre>

<p>Why is it still printing the last number twice? Thanks for your help</p>
