### 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.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182`` ``````#include #include #include #include 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" <> 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 <
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.

 ``12345`` ``````while (file) { file >> file_Number; // use file_Number }``````

 ``1234`` ``````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:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566`` ``````#include #include #include #include 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.