Finding the average number from a file

I am trying to find the average of all the numbers of 2 files (numbers1.txt & more_numbers.txt). The average calculations are always incorrect. The smallest & largest numbers output correctly. Sorry if the codes messy, this is only month 3 of coding for me. Thank you.

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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <climits> 

using namespace std;

int main()
{
    ifstream file;

    int counter = 0;
    
    double file_Number, sum = 0, average = (file_Number / sum), largest = INT_MIN, smallest = INT_MAX;
    
    string file_Name, numbers1 = "numbers1.txt", 
           more_numbers = "more_numbers.txt";
           
    cout << "Which file would you like to open? ";

    cin >> file_Name;

    if (file_Name != numbers1 && file_Name != more_numbers)
    {

        cout << endl;
        
        cout << "Error opening file" <<endl;
        
        return 0;
    }

    else
    {
        file.open(file_Name.c_str());

        file >> file_Number;

        cout << endl;

        cout << "Here are the numbers in the file:" << endl;

        while (file)
        {
            counter++;
            
            cout << counter << ": " << file_Number << endl;

            cout << "";

            file >> file_Number;
            
            if (file_Number > largest)
            {
               
               largest = file_Number; 
            }
		       
		    if (file_Number < smallest)
		    {
		        smallest = file_Number;
		    }
		    
		    else 
		    {
		        sum += file_Number;
		        
		        average = sum / counter;
		    }
        }
        
        cout << endl;

        cout << "There were " << counter << " numbers in the file." << endl;
        
        cout << "The average was: " << average <<endl;
        
        cout << "The highest was: " << largest <<endl;
        
        cout << "The smallest was: " << smallest <<endl;
    }
}
Last edited on
sum = 0, average = (file_Number / sum)
You're dividing by 0 here. So right off the bat your program has undefined behavior.

Please edit your post and add [code] and [/code] around your program to format it.

I suggest, instead of doing
1
2
3
4
5
while (file)
{
    file >> file_Number;
    // use file_Number
}


to instead do:
1
2
3
4
while (file >> file_Number)
{
    // use file_Number
}

This is making it so the loop condition ensures that you have file_Number filled with a valid number from the file.

Edit:
Look at your if-else (lines 59-69). You seem to only be calculating the average if file_number >= smallest. Is that what you want?

Calculate your average at the end, after the loop. You can sum within the loop.
Last edited on
Consider writing a few (reasonably small) functions. For example:

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
#include <iostream>
#include <fstream>
#include <climits>
#include <string>

const int NFILES = 2 ;
const std::string file_names[NFILES] = { "numbers1.txt", "more_numbers.txt" } ;

int get_file_id() // return valid index into he array of file names
{
    std::cout << "file list:\n-----------\n" ;
    for( int i = 0 ; i < NFILES ; ++i ) std::cout << i << ". " << file_names[i] << '\n' ;

    std::cout << "Which file would you like to open [0," << NFILES-1 << "]: " ;
    int file_id ;
    std::cin >> file_id ;

    if( file_id >= 0 && file_id < NFILES ) return file_id ;

    std::cout << "\nerror: invalid input. try again.\n" ;
    return get_file_id() ;
}

void print_stats( std::istream& stm )
{
    int smallest = INT_MAX ; // largest value that an int can hold
    int largest = INT_MIN ; // smallest value that an int can hold
    long long sum = 0 ; // long long: sum of many int values may be outside the range of int
    int cnt = 0 ;

    int number ;
    while( stm >> number ) // for each number read from the file
    {
        ++cnt ;
        if( number < smallest ) smallest = number ;
        if( number > largest ) largest = number ;
        sum += number ;
    }

    std::cout << cnt << " numbers were read\n" ;

    if( cnt > 0 ) // if at least one number was read
    {
        std::cout << "smallest: " << smallest << '\n'
                  << " largest: " << largest << '\n'
                  << " average: " << double(sum) / cnt << '\n' ; // avoid integer division
    }
}

void process_file( int file_id )
{
    if( file_id < 0 && file_id >= NFILES ) return ; // invalid file_id

    const std::string& fname = file_names[file_id] ;

    if( std::ifstream file{fname} ) // if the file was opened for input
        print_stats(file) ;

    else std::cout << "error opening file " << fname << '\n' ;
}

int main()
{
   const int file_id = get_file_id() ;
   process_file(file_id) ;
}
Last edited on
Topic archived. No new replies allowed.