Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Run prgoram chapter4_4 with an empty data file. If the output is in error, modif

ID: 3592045 • Letter: R

Question

Run prgoram chapter4_4 with an empty data file. If the output is in error, modify the program to coreect the error.

chapter4_4 program:

#include <iostream> //Required for cin, cout, and cerr
#include <fstream> //Required for ifstream, and ofstream
#include <string> //Required for string
#include <cstdlib> //Required for return EXIT_SUCCESS

using namespace std;

int main()
{
// Declare and intialize objects
int num_data_pts(0), k;
   double time, motion, sum(0), max, min;
  string filename;
  ifstream sensor3;
  ofstream report;
  
  // Prompt user for name of input file.
  cout << " Enter the name of the input file: ";
  cin >> filename;
  
  // Open report file.
  report.open("sensor3Report.txt");

  // While not at the end of the file.
  // Read and accumulate information
   sensor3 >> time >> motion; //intital input
   while ( !sensor3.eof() )
   {
    num_data_pts++;
    if (num_data_pts == 1)
    {
     max = min = motion;
    }
    sum += motion;
    if (motion > max)
    {
     max = motion;
    }
    if (motion < min)
    {
     min = motion;
    }
    sensor3 >> time >> motion; // input next
   }
   
  // Set format flags
   report.setf(ios::fixed);
   report.setf(ios::showpoint);
   report.precision(2);
   
  //Print summary information
   report << "Number of sensor readings: " << num_data_pts << endl << "Average reading:  "
       << sum/num_data_pts << endl << "Maximum reading  " << max << endl << "Minimum reading:  " << min << endl;

  // Close files and exit program.
   sensor3.close();
   report.close();
  
  return EXIT_SUCCESS;
} //End main

_______________________________________________________________________

I'm not really sure what the ouput is supposed to look like for an empty data file. There woud be no data for it to run. Wouldn't that essentially be like not uploading a file at all? Please help.

Explanation / Answer

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
#include <iostream> //Required for cin, cout, and cerr
#include <fstream> //Required for ifstream, and ofstream
#include <string> //Required for string
#include <cstdlib> //Required for return EXIT_SUCCESS
using namespace std;
int main()
{
// Declare and intialize objects
int num_data_pts(0), k;
double time, motion, sum(0), max, min;
string filename;
ifstream sensor3;
ofstream report;
  
// Prompt user for name of input file.
cout << " Enter the name of the input file: ";
cin >> filename;
  
// Open report file.
report.open("sensor3Report.txt");
sensor3.open(filename);
if (!sensor3)
{
cout << "can't find " <<filename<< endl;
exit(0);
}
std::ifstream myfile(filename, std::ios::ate);
if (myfile.tellg() == 0)
{
cout<<"File is Empty: "<<endl;
exit(0);
  
}

// While not at the end of the file.
// Read and accumulate information
sensor3 >> time >> motion; //intital input
while ( !sensor3.eof() )
{
num_data_pts++;
if (num_data_pts == 1)
{
max = min = motion;
}
sum += motion;
if (motion > max)
{
max = motion;
}
if (motion < min)
{
min = motion;
}
sensor3 >> time >> motion; // input next
}

// Set format flags
report.setf(ios::fixed);
report.setf(ios::showpoint);
report.precision(2);

//Print summary information
report << "Number of sensor readings: " << num_data_pts << endl << "Average reading: "
<< sum/num_data_pts << endl << "Maximum reading " << max << endl << "Minimum reading: " << min << endl;

// Close files and exit program.
sensor3.close();
report.close();
  
return EXIT_SUCCESS;
} //End main


===========
I have added condition to check if file is empty or if the file is not found