HI, I am trying to calculate the average, max and min values from a text file but they aren't quite working. Can someone please check to see the errors and I also have to calculate the standard deviation but I'm stuck here haven't finished the code yet. thanks in advance
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
usingnamespace std;
int main ()
{
ifstream fin;
ofstream fout;
string file_input, file_output;
int num, count = 0, total =0;
float avg, Min = 1000, Max=0;
cout << "\nThis program will produce statistics (Mean, Standard Deviation, Maximum and Minimum values of the list) for a list of integer values. The user will provide the names of input and output files." << endl;
cout <<"\nEnter name and location for input file: " ;
getline(cin, file_input);
cout <<"Enter name and location for output file: ";
getline(cin, file_output);
fin.open( file_input.c_str());
if (fin.fail())
{
cout << "Bad file name or location.\n" ;
exit(0);
}
cout << "\nReading values for first time. . ." <<endl;
fout << "\nReading values for first time. . ." <<endl;
fin >> num;
while(!fin.eof())
{
cout << num << ' ';
if(++count%10 == 0)
cout << endl;
fin >> num;
}
while (!fin.eof())
{
total += num;
count++;
if (num > Max)
Max = num;
if (num < Min)
Min = num;
fin >>num;
}
avg = total/count;
cout << endl;
cout << " \nMean of the values : " << avg << endl;
cout << " Greatest value : " << Max << endl;
cout << " Least value : " << Min << endl;
return 0;
}
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <limits> // std::numeric_limits<>
usingnamespace std;
int main ()
{
/*
ifstream fin;
ofstream fout;
string file_input, file_output;
int num, count = 0, total =0;
float avg, Min = 1000, Max=0;
*/
cout << "\nThis program will produce statistics (Mean, Standard Deviation, ""Maximum and Minimum values of the list) for a list of integer values.""The user will provide the names of input and output files.\n" ; // << endl;
cout <<"\nEnter name and location for input file: " ;
string file_input ;
getline(cin, file_input);
// fin.open( file_input.c_str());
ifstream fin( file_input.c_str() ) ; // let the constructor open it
if(fin.fail())
{
cout << "Bad file name or location.\n" ;
// exit(0);
return 0 ;
}
cout <<"Enter name and location for output file: ";
string file_output ;
getline(cin, file_output);
// fout.open( file_input.c_str());
ofstream fout( file_output.c_str() ) ; // let the constructor open it
cout << "\nReading values for first time. . .\n" ; // <<endl;
fout << "\nReading values for first time. . .\n" ; // <<endl;
int num ;
int count = 0 ;
int total = 0 ;
int Min = numeric_limits<int>::max() ; // smallest possible value
int Max = numeric_limits<int>::min() ; // largest possible value
// fin >> num;
// while(!fin.eof())
while( fin >> num ) // canonical
{
cout << num << ' ' ;
fout << num << ' ' ;
if( ++count%10 == 0 )
{
cout << '\n' ; //endl;
fout << '\n' ;
}
//fin >> num;
total += num ;
if( num > Max ) Max = num ;
if( num < Min ) Min = num ;
}
/*
while (!fin.eof())
{
total += num;
count++;
if (num > Max)
Max = num;
if (num < Min)
Min = num;
fin >>num;
}
*/
if( count > 0 )
{
constdouble avg = double(total) / count ; // avoid integer division
//cout << endl;
cout << " \n\nMean of the values : " << avg << '\n' ; // endl;
cout << " Greatest value : " << Max << '\n' ; // endl;
cout << " Least value : " << Min << '\n' ; // endl;
fout << " \n\nMean of the values : " << avg << '\n' ; // endl;
fout << " Greatest value : " << Max << '\n' ; // endl;
fout << " Least value : " << Min << '\n' ; // endl;
}
//return 0;
}