Hi, I forgot how to do the decimal precision aka magic formula
Like after you run the program it give you 4 decimal places but i only want 2 decimal places
Hope someone can help!!
Thank You!!
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
void cal_avg(ifstream& input, ofstream& output);
int main()
{
ifstream input;
string inf;
ofstream output;
string outf;
cout << "Please enter the input file name that you want to open: " << endl;
cin >> inf;
cout << "Please enter the output file name that you want to open: " << endl;
cin >> outf;
input.open(inf.c_str());
output.open(outf.c_str());
if(input.fail() && output.fail())
{
cout << "The input file " << inf << "and the output file" << outf << "could not be opened!" << endl;
exit(1);
}
cal_avg(input, output);
input.close();
output.close();
return 0;
}
void cal_avg(ifstream& input, ofstream& output)
{
double val, sum=0, avg=0;
int i=0;
while(input >> val)
{
sum += val;
i++;
}
avg = sum / i;
cout << "The average is: " << avg << endl;
output << "The average is: " << avg << endl;
}