Please make this code easy to understand This is suppose to
Please make this code easy to understand. This is suppose to be a low level comp sci course so don\'t use advanced programming. Use try and catch statements, expections, file I/O.
Write a program that reads a stream of integers from a file and prints to the screen the range of integers in the file (i.e. [lowest, highest]). You should first prompt the user to provide the file name. You should then read all the integers from the file, keeping track of the lowest and highest values seen in the entire file, and only print out the range of values after the entire file has been read.
Importantly, unlike the previous problem, you can make no assumptions about the contents of the file. If your program cannot read the file, opens an empty file, or encounters a non-integer while reading the file, your program should output that the file is invalid.
Test for several invalid files (e.g. non-existent, empty, non-integer) as well as files that contain only integer values.
Solution
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char character;
int number = 51;
int count = 0;
ofstream out_stream;
ifstream in_stream1; /* Stream for counting integers. */
ifstream in_stream2; /* Stream for counting characters. */
/* Create the file */
out_stream.open(\"Integers\");
for (count = 1 ; count <= 5 ; count++)
out_stream << number++ << \' \';
out_stream.close();
/* Count the integers in the file */
in_stream1.open(\"Integers\");
count = 0;
in_stream1 >> number;
if (!in_stream1.eof())
{
count++;
in_stream1 >> number;
}
in_stream1.close();
cout << \"There are \" << count << \" integers in the file,\ \";
else
{
cout << \"There are no files \" << count << \" invalid file,\ \";
}
/* Count the non-blank characters */
in_stream2.open(\"Integers\");
count = 0;
in_stream2 >> character;
if (!in_stream2.eof())
{
count++;
in_stream2 >> character;
}
in_stream2.close();
cout << \"represented using \" << count << \" characters.\ \";
else
{
cout << \"There are no files \" << count << \" invalid file,\ \";
}
return 0;
}


