#include <iostream> #include <fstream> #include <string> using namespace std; in
ID: 3591387 • Letter: #
Question
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
//Declare and initialize objects.
int num_data_pts(0), k;
double time, motion, sum(0), max, min;
string file nname;
ifstream sensor3;
ofstream report;
// Prompt user for name of input file.
cout << " Enter the name of the input file";
cin >> filename;
//open file and read teh first data point.
sensor3.open(filename.c_str());
if (sensor3.fail())
{
cerr << "Enter opening input file ";
exit(1);
}
// open report file.
report.open("sensor3Report.txt") ;
//While not at the end of the file.
//Read and accumulate information
sensor3 >> time >> motion; //initial input
while ( !sensor3.eof() )
{
num_data_pts++;
if (num_data_pts == 1)
{
max = min = motion;
}
sum +=motion;
if motion (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 readings : "
<< sum/num_data_pts << endl
<< "Maximum reading: "
<< max << endl
<< "Minimum reading: "
<< min << endl;
//Close file and exit program.
sensor3.close() ;
report.close() ;
return0 ;
} //end main
Sensor3.dat
0 132.5
0.1 147.2
0.2 148.3
0.3 157.3
0.4 163.2
0.5 158.2
0.6 169.3
0.7 148.2
0.8 137.6
0.9 135.9
Sensor3Report.txt
Number of sensor readings: 10
Average raeding: 149.77
Maximum reading: 169.30
Minimum reading: 132.50
1.Run the program with an empty data file. If the output is in error, modify the program to correct the error
Important Note
Instead of exit(1); use exit(EXIT_FAILURE); and instead of return 0; use return EXIT_SUCCESS;
also, show the output
Explanation / Answer
//Please see the updated code below:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
//Declare and initialize 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 file and read teh first data point.
sensor3.open(filename.c_str());
if (sensor3.fail())
{
cerr << "Enter opening input file ";
exit(EXIT_FAILURE);
}
// open report file.
report.open("sensor3Report.txt") ;
//While not at the end of the file.
//Read and accumulate information
sensor3 >> time >> motion; //initial 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 readings : "
<< sum/num_data_pts << endl
<< "Maximum reading: "
<< max << endl
<< "Minimum reading: "
<< min << endl;
//Close file and exit program.
sensor3.close() ;
report.close() ;
return EXIT_SUCCESS;
} //end main
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.