Skip to content
Snippets Groups Projects
mean_userdefined.cpp 651 B
Newer Older
#include <iostream>

class ave{
public:
  ave(): ncount_(0), rsum_(0), amean_(0) {};

  ave & operator+=(int d){
    rsum_+=d;
    ncount_++;
    double stemp=static_cast<double>(rsum_);
    double ntemp=static_cast<double>(ncount_);
    amean_=stemp/ntemp;
    return *this;
  }

  double get_ave() const { return amean_;}


private:
    int ncount_;
    int rsum_;
    double amean_;

};


int main(){

  ave a;
  int indata{0};
  std::cout <<"Enter values [Ctrl-D to finish]: " <<std::endl;

  while(std::cin>>indata){
    a+=indata;
  }

  std::cout << "Average: " << a.get_ave() << std::endl;
  return 0;
}