// reading text data into a program from a file
//Barb Sanchez
//9-11-02

#include <iostream>
#include <fstream> // for file i/o
#include <iomanip> // for alignment

using namespace std;

int main()
{
    int count;
    float x, sum, average;

    ifstream data_in; //declare object for reading in file

    sum = 0;
    count = 0;

    data_in.open ("floats.dat", ios::in); //open file for input
    
    if (data_in)  //if no error, open file
    {
        
        cout << "The numbers in the data file are: \n";

        do 
        {

            data_in >> x;
            cout << "The number coming in is: "<< x << endl;
            sum = sum + x; // add to running total
            count++; //add one to count

        } while (x!=0);

    
    average = sum / (count-1); // calculate average

    //output final sum and average to screen

    cout.setf(ios::fixed);

    cout << "The sum of the numbers is: " << sum << endl;
    
    cout << "The average of the numbers is (does not include the 0)"
         << setprecision(2) <<average << endl;

    } //if

    else
    {
        cout << "An error occured while opening the file.\n";
    } 

    data_in.close();

    return 0;
} // main