简单计算不提供正确的值

时间:2015-09-02 12:31:31

标签: c++ variables int double global

我是C ++的新手,对变量类型及其对不同操作的影响感到困惑。我有一个简单的代码,我尝试做一些操作,但输出不是我期望的值。我很感激任何关于我可能做错的评论 - 我认为这与我定义我的变量(全局/本地)及其类型的方式有关。谢谢你的支持。

#include <iostream>
#include <fstream>
#include <set>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <sstream>
#include <numeric>

using namespace std;
void save_output()
{
   const int npart=1;
   vector<double>sx_1(npart);
   double col1;
   FILE * sortie;
   ifstream m1("input.txt");
   string l1;

   int c1=0;
   while (getline(m1,l1)){
      c1++;
      istringstream ss(l1);
      ss>>col1;
      sx_1[c1]=col1;
   }

   const double e1=0.005;
   double SX_1=0; 
   SX_1=accumulate(sx_1.begin(),sx_1.end(),0);

   double d1_1=0;
   d1_1=(1/npart)*SX_1/e1; 

   sortie = fopen("output", "a");
   fprintf(sortie,"%.2f", d1_1);
   fclose(sortie);
}

int main()
{
   save_output();
}

0 个答案:

没有答案
相关问题