C++ 需要帮助计算增益。遇到错误。需要帮助吗

C++ 需要帮助计算增益。遇到错误。需要帮助吗,c++,C++,我的程序是一个自动股票市场,它从文件中读取数据,然后在屏幕上打印出来或写入文件。我能够读取文件并将其显示在屏幕上,但在计算增益时遇到错误。 下面是我的代码: istream& operator>>(istream& ins, stockType& stock) {//member variables are all declared as a double ins>>stock.todays_open_price >>

我的程序是一个自动股票市场,它从文件中读取数据,然后在屏幕上打印出来或写入文件。我能够读取文件并将其显示在屏幕上,但在计算增益时遇到错误。 下面是我的代码:

istream& operator>>(istream& ins, stockType& stock)
 {//member variables are all declared as a double
   ins>>stock.todays_open_price
      >>stock.todays_close_price
      >>stock.todays_high_price
      >>stock.prev_low_price
      >>stock.prev_close_price;
      calculateGain(stock.todays_close_price, stock_prev_close_price);
      return ins;
  }

 void stockType::calculateGain(double close, double prev)
        {  // gain was declared in the header file as a private member
           //variable to store the gain calculated.
           gain = ((close-prev)/(prev));
         }
  ostream& operator<<(ostream& outs, const stockType& stock)
  {        
     outs<<stock.getOpenprice()
         <<stock.getCloseprice()
         <<stock.getPrevLowPrice()
         <<stock.getPrevClosePrice()
         <<stock.getGain()
         return outs
   }

    //double getGain() was declared in the header file also as
     double getGain() {return gain;}
istream和operator>>(istream和ins、库存类型和库存)
{//成员变量都声明为双精度
ins>>股票。今日\开盘价
>>股票。今天收盘价
>>股票。今天价格高
>>股票。上一个低价格
>>stock.prev\u收盘价;
计算(股票今天收盘价、股票上收盘价);
返回ins;
}
无效股票类型::计算收益(双收盘,双上)
{//gain在头文件中声明为私有成员
//变量来存储计算的增益。
收益=((收盘价)/(收盘价));
}

ostream&operator函数
calculateGain
是类
stockType
的成员;这是
stockType
的一个实例可以做的事情。重载的
运算符>>
(它不是
stockType
的成员)在没有此类实例的情况下调用它,这是非法的。试试这个:

istream& operator>>(istream& ins, stockType& stock)
{
  ...
  stock.calculateGain(stock.todays_close_price, stock.prev_close_price);
  ...
}

它是stockType的成员。我在头文件中声明了它。hello beta,如果我想在calculateGain()范围内将增益四舍五入到2个小数点。我该怎么走that@Emy:1)学习将
双精度
转换为
整数
,注意它会截断;2)学习使用此函数进行四舍五入,而不是截断;3)学习四舍五入到最接近的0.01,而不是最接近的整数。当我从文件80.00中读取double时,屏幕上显示80。但我希望它显示80.00。你是怎么做到的?感谢早些时候的收益,例如8.36474。但我希望增益显示为8.36或8.40