Write a program that will search a file of numbers of type int and write the largest and the smallest numbers to the screen. The file contains nothing but numbers of type int separated by blanks or line breaks.
Complete Program:
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <climits>
using namespace std;
int main()
{
string fileName = "";
int number;
int largestNumber = INT_MIN;
int smallestNumber = INT_MAX;
cout << "Enter the file name: ";
cin >> fileName;
ifstream infile;
infile.open(fileName);
if (infile.fail())
{
cout << fileName << " file cannot be opened!" << endl;
exit(1);
}
while (infile >> number)
{
if (number > largestNumber)
largestNumber = number;
if (number < smallestNumber)
smallestNumber = number;
}
cout << "The largest number in the file: " << largestNumber << endl;
cout << "The smallest number in the file: " << smallestNumber << endl;
return 0;
}
Input file: indata.txt
Output on console: