Question: // Program IODemo demonstrates how to use EOF #include #include using namespace std; int main() { cout < < fixed < < showpoint; float val;

// Program IODemo demonstrates how to use EOF #include #include using namespace std; int main() { cout << fixed << showpoint; float val; float sum = 0; float average = 0; int count = 0; ifstream inData; // declares input stream ofstream outData; // declares output stream // binds program variable inData to file "inputfile.txt" inData.open("inputfile.txt"); //Testing the state of the stream //true means the last I/O operation on that stream succeeded //false means the last I/O operation on that stream failed if (!inData) { cout << "Can't open the input file successfuly." << endl; return 1; } // binds program variable outData to file "outputfile.txt" outData.open("outputfile.txt"); //Testing the state of the stream if (!outData) { cout << "Can't open the output file successfuly." << endl; return 2; } //Read value from the input file //Loop terminates when EOF is encountered cout << "The initial value for count is " << count << ". " << endl; cout << "Starting to process input file ... " << endl; inData >> val; //read in the first value while (inData) //while previous read succeeded ... { // Add code: add the newly read value to the variable "sum" // Add code: increment the counter "count". // Add code: read the next value from the file into the variable "val" } if (count != 0 ) average = sum / count; else average = 0; // outputs sum, count and average outData << "The sum of the input values is " << sum << "." << endl; outData << "The number of valid input values is " << count << "." << endl; outData << "The average is " << average << "." <

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!