Read floating values from the file inputfile.txt and write the sum,average,and number of valid input values to the output file outputfile.txt. I have most of it figured out, I am just struggling with the while loop, any help is appreciated.
Input file: 8.9 2.6 5.3
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
cout << fixed << showpoint;
float val;
float sum = 0;
float average = 0;
int count = 0;
ifstream inData;
ofstream outData;
inData.open("inputfile.txt");
if (!inData)
{
cout << "Can't open the input file successfuly." << endl;
return 1;
}
outData.open("outputfile.txt");
if (!outData)
{
cout << "Can't open the output file successfuly." << endl;
return 2;
}
cout << "The initial value for count is " << count << ". " << endl;
cout << "Starting to process input file ... " << endl;
inData >> val;
while (inData)
{
sum = sum + val;
count++;
inData >> val;
}
if (count != 0 )
average = sum / count;
else
average = 0;
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 << "." <<endl;
cout << "Output has been placed in the file: outputfile.txt" << endl;
return 0;
}