Answer:
#include <fstream> //class ofstream in the fstream
#include <string>
#include <cstdlib> // for the exit()
#include <iostream>
using namespace std;
int main()
{
ofstream foutstr;// for declaring the output file stream
ifstream finstr; // for declaring the input file stream
int x(0);
double average(0.0);
string filename="number.dat";
int total(0);
// open the file for inputing
finstr.open("C:\\Users\\sam\\OneDrive\\Documents\\Visual Studio 2017\\Projects\\Project sam\\\umber.dat", ios::in);
// check whether the file can be opened or not
if (!finstr.is_open())
{
cerr << "Not able to open the file " << filename << endl;
exit(10);
}
// read text from file
fin >> x;
int a(0);
int b(0);
int largest=0;
while (!fin.fail())
{
cout << "Read integer: " << x << endl;
fin >> x;
if (Largest<x)
{
Largest=x;
}
total += x;
if (x%2 == 0)
{ a++; }
else
{ b++;}
}
avg = sum/(a+b);
cout << "The stored integer's average is:" << avg << endl;
cout << "The largest number in the file is:"<<largest<<endl;
// check whether there is any error!
if (!finstr.eof())
{
cerr << "Error reading file " << name << endl;
exit(20);
}
// close file stream finstr
finstr.close();
return 0;
}
Step-by-step explanation:
#include <fstream> //class ofstream in the fstream
#include <string>
#include <cstdlib> // for the exit()
#include <iostream>
using namespace std;
int main()
{
ofstream foutstr;// for declaring the output file stream
ifstream finstr; // for declaring the input file stream
int x(0);
double average(0.0);
string filename="number.dat";
int total(0);
// open the file for inputing
finstr.open("C:\\Users\\sam\\OneDrive\\Documents\\Visual Studio 2017\\Projects\\Project sam\\\umber.dat", ios::in);
// check whether the file can be opened or not
if (!finstr.is_open())
{
cerr << "Not able to open the file " << filename << endl;
exit(10);
}
// read text from file
fin >> x;
int a(0);
int b(0);
int largest=0;
while (!fin.fail())
{
cout << "Read integer: " << x << endl;
fin >> x;
if (Largest<x)
{
Largest=x;
}
total += x;
if (x%2 == 0)
{ a++; }
else
{ b++;}
}
avg = sum/(a+b);
cout << "The stored integer's average is:" << avg << endl;
cout << "The largest number in the file is:"<<largest<<endl;
// check whether there is any error!
if (!finstr.eof())
{
cerr << "Error reading file " << name << endl;
exit(20);
}
// close file stream finstr
finstr.close();
return 0;
}