C++取负整数的平均值

C++取负整数的平均值,c++,integer,average,C++,Integer,Average,您好,我已经在这个程序上工作了一段时间了,似乎无法通过批处理获得数据文件中只有负整数的平均值的正确答案。 这是我到目前为止所拥有的 #include<iostream> #include<iomanip> using namespace std; int main() { int data;// the data which will be inputted from the file int count = 0

您好,我已经在这个程序上工作了一段时间了,似乎无法通过批处理获得数据文件中只有负整数的平均值的正确答案。 这是我到目前为止所拥有的

 #include<iostream>
 #include<iomanip>
 using namespace std;
 int main()
 {
  int data;// the data which will be inputted from the file                     
  int count = 0;// # of integers in data file                                   
  int amount = 0; // number of data integers in a column                        
  int negatives = 0; // number of negative integers in file                     
  double average = 0; // average of negative numbers in file                    
  int  sum= 0; // sum of negative values in file                                
  double value; // value of negative numbers in file                            
  cin >> data;
  cout << "Values read from file" << endl;
  while (cin >> data)
  count++;
   amount++;
   cout << setw(10) <<  data;
   if ((amount % 5)  == 0)
     {
       cout << endl;

     }
   if ( data < 0)
   {
     negatives++;
     value  = data;
     average =  value/ negatives;

   }
   else
     if (data > 0)
       {
         cout <<  "There was no negative #s" << endl;
       }
}  if (amount)
{
  cout << endl;
}
 cout << "# of values read: " << count << endl;
 cout << "Average of negative #s:" << average << endl;
 return 0;
}

因此,程序的要点是以5列显示输入文件中的数据,然后显示文件中的整数数,然后取文件中负数的平均值。如果文件中没有负数,程序必须显示一条消息,说明没有负数。就整数计数的输出和5列的显示而言,一切看起来都很好,但随后我在文件中输入了负数平均值的代码,结果将输出完全搞乱了

把牙套修好。在调用>>数据时,唯一循环的是后面的一行count++;输入文件?那你呢?我认为你失去了一个支撑。{括号出现在实际编译的程序中,但编译不正确。您使用过调试器并单步执行代码吗?如果您拒绝或没有这样做,我真的不想在代码上使用调试器。您没有对数据求和。您正在将读取的值除以负数。Pardon我对求和和和平均的理解,但我认为求和就是把所有的数字相加,平均就是求和除以求和的数字的数量。不过我可能错了。