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; }



LinkBack URL
About LinkBacks


