How to display the min and max values of integers read from a file?
<p>Below is my code. I have a problem in my alogrithm. It displays the last value of the integer from the input file for both the max and min. values. Would someone please take a look and tell me what I'm doing wrong?</p>

<pre><code>#include "cstdlib"
#include "iostream"
#include "fstream"

using namespace std;

int main()
{
    fstream instream;
    ofstream outstream;
    instream.open("num.txt");
    if(instream.fail())
    {
        cout&lt;&lt;"The input file failed to open\n";
        exit(1);
    }
    outstream.open("output.txt");
    if(outstream.fail())
    {
        cout&lt;&lt;"The output file failed to open";
        exit(1);
    }

    int next, largest, smallest;
    largest = 0;
    smallest = 0;

    while(instream&gt;&gt;next)
    {
        largest = next;
        smallest = next;
        if(largest&lt;next)
        {
            largest = next;
        }
        if(smallest&gt;next)
        {
            smallest = next;
        }
    }

    outstream&lt;&lt;"The largest number is: "&lt;&lt;largest&lt;&lt;endl;
    outstream&lt;&lt;"The smallest number is: "&lt;&lt;smallest&lt;&lt;endl;
    instream.close();
    outstream.close();
    return 0;
}
</code></pre>
