Code Error!!!

closed account (3796URfi)
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace 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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <limits> // std::numeric_limits<>

using namespace 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 )
    {
        const double 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;
}
closed account (3796URfi)
thankyou for your help
Topic archived. No new replies allowed.