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;
}

Code seems to work fine for me except that if there is not a new line at the end of the last number in the list your first loop produces a count that is 1 too small.

@Banta
Are you sure? It doesn't seem to work with me. The count value (when i print it) in the second loop comes out the same as the first one ! there is a new line in the text file ofcourse
please help

I think you need to supply a sample of the input data you're using and a sample of the data output to the file and an explanation of what exactly is or isn't right.

For instance when I use this data:

1
2
3
4
5

I get this output:

Reading values first time. . .
1 2 3 4 

Number of values read: 4
                    Mean of the values: 2.500
     Standard deviation using method 1: 1.118
                        Greatest value: 4
                           Least value: 1

Reading values second time. . .


Number of values read: 5

    Standard Deviation using method 2: 2.449

I don't see a the doubling that you describe, but I do see that the first count doesn't include the last element whereas the second count does.

This is a sample output of the program:
the second loop should count every 3 numbers of the list and the count isn't working for me
(whole assignment is attached) I don't know what is wrong

SAMPLE OUTPUT :

This 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.
Enter the name of the input file: STATNUMS.DAT Enter the name of the output file: STATNUMS.out
Reading values first time. . .
10 11 8 9 6 21 54 89 2 15
34 5  8
Number of values read: 13
                    Mean of the values : xxx.xxx
      Standard deviation using method 1: xxx.xxx
                        Greatest value : xxx.xxx
                           Least value : xxx.xxx
Reading values second time
10 9 54 15 8
Number of values read : 5
      Standard deviation using method 2: xxx.xxx
End of program.

@tinstaafl

there are a few problems.

One is you're checking for every third number, but count is counting every number.

Two the calculations aren't in the if block executed on every third number

Three with count starting at 0 for the second read you miss the first number.

Here's some modified code which should do the job:

//Force the calculations to use the first number
count = -1;
while(!fin.eof())
{
    while(fin.peek() == '\n')
        fin.ignore(1,'\n');
    fin >> num;
    if(++count%3 == 0)
    {
        cout << setw(3) << num;
        fout << setw(3) << num ;
        double totalSq=0, avg =0;
        total += num;
        totalSq+= (num-avg)*(num-avg);
    }
}
//Adjust count to reflect how many numbers you used.
count = (int)(count/3) + 1;

As for the problem of ignoring empty lines something like this should work:

double totalSq=0;   
while(!fin.eof())
{
    while(fin.peek() == '\n')
        fin.ignore(1,'\n');
    fin >> num;
    cout << setw(3) << num;
    fout << setw(3) << 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.close();
const double avg = double (total) / count ;

Thankyou for you help, I really appreciate it. One more question, Do you think there is anything wrong with the placement of the formulas because my second standard deviation isn't calculating correctly, I just noticed!

double totalSq=0, avg =0;
total += num;
totalSq+= (num-avg)*(num-avg);

In the second loop:
Isn't the declaration of double totalSq=0, avg =0; only in scope for this loop? Shouldn't these values just be initialised before the loop and const removed from the initial declaration of avg - double avg = static_cast<double>(total) / count ;
How is avg in this loop mean't to be calculated? At present its value will always be 0.

@nullptr yes, I have noticed there was something wrong because the second standard deviation doesn't have valid calculations, thankyou I will solve the issue

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.