Thread: need help please

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    494

    need help please

    ok i tryed something else and it works a little, so far i have managed to read the numbers and find the average but need help with displaying the numbers contained the file, can anyone hep me.


    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    //class Array
    class Array
    {
    public:
    	float   findaverage();
    
    private:
    		int             x;
    	    int           sum;
    		float     average;
    };
    float Array::findaverage()
    {
    	int      sum = 0;
        int            x;
    	float average=0.0;
        ifstream infile;
        
    	//open the file
        infile.open("array.txt");
        if (!infile) {
            cerr << "Cannot open file";
            exit(1); 
        }
        //find average
        while (infile >> x) {
            sum +=  x;
    		average =sum/x;
        }
        
        infile.close();
    
        cout << "The average is  " << average << endl; 
        return 0;
    }
    int main() 
    {
    		Array myarray;
    		myarray.findaverage();
    		return 0;
        
    }
    When no one helps you out. Call google();

  2. #2
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    >>need help with displaying the numbers contained the file
    I'm not sure I understand what you want. Do you want to print the numbers in the file before returning their average?
    Code:
    while (infile >> x) {
        cout<< x <<endl;
        sum +=  x;
        average =sum/x;
    }
    Kampai!

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    yep that was it, thanks Sake
    When no one helps you out. Call google();

Popular pages Recent additions subscribe to a feed