The second loop after opening the file again the second calculation for count isn't coming out correctly it seems to be doubled, comes out double the normal value when it shouldn't someone help please!
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>
using namespace std;
int main ()
{
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" ;
cout <<"\nEnter the name and location of the input file: " ;
string file_input ;
getline(cin, file_input);
ifstream fin( file_input.c_str() ) ;
if(fin.fail())
{
cout << "Bad file name or location.\n" ;
exit(0);
}
cout <<"Enter the name and location of the output file: ";
string file_output ;
getline(cin, file_output);
ofstream fout( file_output.c_str() );
cout << "\nReading values first time. . .\n" ;
fout << "\nReading values first time. . .\n" ;
int num;
int count = 0 ;
double total = 0 ;
int Min = 100;
int Max = 0;
double totalSq=0;
fin >> num;
while(!fin.eof())
{
cout << num << ' ' ;
fout << num << ' ' ;
if( ++count%10 == 0 )
{
cout << '\n' ;
fout << '\n' ;
}
{
total += num ;
if( num > Max ) Max = num ;
if( num < Min ) Min = num ;
totalSq+= num*num;
fin >> num;
}
}
const double avg = double (total) / count ;
if (count>0)
{
double stdDev = sqrt((totalSq/count) - pow(avg,2));
cout << fixed << setprecision(3);
cout << "\n\nNumber of values read: " << count << endl;
cout << setw(40) << right << "Mean of the values: " << avg << endl;
cout << setw(40) << right << "Standard deviation using method 1: " << stdDev << endl;
cout << setw(40) << right << "Greatest value: " << Max << '\n' ;
cout << setw(40) << right << "Least value: " << Min << '\n' ;
fout << fixed << setprecision(3);
fout << "\n\nNumber of values read: " << count << endl;
fout << setw(40) << right << "Mean of the values: " << avg << endl;
fout << setw(40) << right << "Standard deviation using method 1: " << stdDev << endl;
fout << setw(40) << right << "Greatest value: " << Max << '\n' ;
fout << setw(40) << right << "Least value: " << Min << '\n' ;
fin.close();
}
fin.open(file_input.c_str());
cout << "\nReading values second time. . ." << endl;
fout << "\nReading values second time. . ." << endl;
count = 0;
while(fin >> num)
{
if(++count%3 == 0)
{
cout << num << ' ' ;
}
{
double totalSq=0, avg =0;
total += num;
totalSq+= (num-avg)*(num-avg);
}
}
if (count > 0)
{
double stdDev = sqrt(totalSq/count);
cout << "\n\nNumber of values read: " << count << endl;
fout << "\n\nNumber of values read: " << count << endl;
cout << setw(40) << right << "\n\tStandard Deviation using method 2: "<< stdDev <<endl;
fout << setw(40) << right << "\n\tStandard Deviation using method 2: "<< stdDev <<endl;
}
return 0;
}