简体   繁体   English

错误:C ++中类型为“ float”和“ int”的二进制“ operator%”类型的无效操作数

[英]error: invalid operands of types 'float' and 'int' to binary 'operator%' in c++

Just started programing in C++ a few weeks ago and I encountered this problem: 几周前刚开始用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;

Ideally, the program should display on the screen the number "f" representing the arithmetic average. 理想情况下,程序应在屏幕上显示代表算术平均值的数字“ f”。

The % operator is not defined for real numbers - you want the fmod() function declared in <cmath> . 未为实数定义%运算符-您需要在<cmath>声明fmod()函数。 See http://en.cppreference.com/w/cpp/numeric/math/fmod 参见http://en.cppreference.com/w/cpp/numeric/math/fmod

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:二进制运算符&#39;/&#39;的类型为&#39;int&#39;和&#39;float *&#39;的无效操作数 - Error: invalid operands of types 'int' and 'float*' to binary operator '/' 错误:类型为“int [1]”和“float”的无效操作数转换为二进制“operator*” - error: invalid operands of types 'int [1]' and 'float' to binary 'operator*' 错误消息:“float”和“int”类型的无效操作数转换为二进制“operator%” - Error Message : Invalid operands of types 'float' and 'int' to binary 'operator%' 错误:二进制“ operator *”类型为“ float”和“ float [0]”的无效操作数 - Error: invalid operands of types 'float' and 'float[0]' to binary 'operator*' 'float*' 和 'float*' 类型的无效操作数到二进制 'operator*' - Invalid operands of types 'float*' and 'float*' to binary 'operator*' 'int' 和 'int [3]' 类型的无效操作数到二进制 'operator*' - invalid operands of types ‘int’ and ‘int [3]’ to binary ‘operator*’ 错误:类型为&#39;const char [3]&#39;和&#39;int *&#39;的无效操作数为二进制&#39;operator *&#39; - Error: invalid operands of types 'const char [3]' and 'int*' to binary 'operator*' “错误:二进制&#39;operator &lt;&lt;&#39;|的类型为&#39;int&#39;和&#39;const char [2]&#39;的无效操作数” - “error: invalid operands of types 'int' and 'const char [2]' to binary 'operator<<'|” 二进制“ operator%”的类型为“ double”和“ int”的无效操作数 - invalid operands of types `double' and `int' to binary `operator%' int类型的无效操作数和二进制&#39;operator%&#39;的double - invalid operands of types int and double to binary 'operator%'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM