c++ - 错误 : invalid operands of types 'float' and 'int' to binary 'operator%' in c++

标签 c++

几周前刚开始用 C++ 编程,我遇到了这个问题:

float a,i,b,c,d,e,f;
cin>>a;
cin>>c;
if(c%2==0)
{
    d=c;
    e=1;
}
else
{
    d=0;
    e=0;
}

for (i=2;i<=a;i++)
{
     cin>>b;
     if(b%2==0)
     {
         d=d+b;
         e=e+1;
     }
}
f=d/e;
if(e==0)
    cout<<"0";
else
    cout<<fixed<<setprecision(2)<<f;

理想情况下,程序应在屏幕上显示代表算术平均值的数字“f”。

最佳答案

% 运算符未针对实数定义 - 您需要在 <cmath> 中声明的 fmod() 函数.参见 http://en.cppreference.com/w/cpp/numeric/math/fmod

关于c++ - 错误 : invalid operands of types 'float' and 'int' to binary 'operator%' in c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41904080/

相关文章:

c++ - Eclipse 中的 C/C++ 方法参数提示

c++ - 是否可以重新分配 std::unique_ptr 以使其旧值在构造新值之前被销毁?

c++ - Eigen 库 : Take a copy of a templated function parameter

c++ - 查找 vector 中最接近的值

c++ - 更有效地使用确定用户输入获胜者的功能

c++ - 在另一个线程 visual studio 2012 中看不到 std::string 的变化

c++ - 系统托盘应用 Linux Qt/C++

C++ OTL 看不到外部数据库更改

c++ - 你如何声明一个 extern "C"函数指针

c++ - 如何从 vtk 数据中删除字形?