错误:"表达式必须具有整数或未整合的枚举类型"

Vic*_*ins 3 c++ math enums operators modulus

那些家伙我是新手我尝试查找解决方案但是,我并没有真正理解它,我正在写速记以了解如何用其他代码替换它们,所以我遇到了模数我添加但是它给出了 "表达式必须具有整数或未整合的枚举类型".

我不知道枚举类型是否正确,它们代码不运行?

#include<iostream>
#include<string>
using namespace std;

int main() {

    double b, x, y, z, a, c;

    c, b, x, y, z, a, c = 100;
    x += 5;
    y -= 2;
    z *= 10;
    a /= b;
    c %= 3; // "c" seems to be giving out that error?


    cout << b << x << y << z << a << c;


    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这里的问题是"c"给出"表达式必须具有整数或未整合的枚举类型"错误.

我知道模数的作用,它给出了2个数之间除数的余数,但是我在这种情况下难以理解,因为它应该给出余数吗?它在语法上是错误的吗?

gsa*_*ras 10

c是double,因此你不能使用modulo运算符%.

请改用fmod().

所以改变这个:

c %= 3
Run Code Online (Sandbox Code Playgroud)

对此:

c = fmod(c, 3);
Run Code Online (Sandbox Code Playgroud)

正如Slava提到的,你可以使用一个int代替,就像这样:

int c = 5; // for example
c %= 3
Run Code Online (Sandbox Code Playgroud)

这不需要使用fmod().重要的是要理解模块运算符%ints一起工作.


正如πάνταρέι提到的,还有这样的:不能在双打上使用模数?


作为旁注Victor,你有很多变量,但大多数是未使用的或未初始化的.编译时是否启用了所有警告?这是我在编译原始代码时所得到的(通过注释生成错误的行):

C02QT2UBFVH6-lm:~ gsamaras$ g++ -Wall main.cpp 
main.cpp:9:5: warning: expression result unused [-Wunused-value]
    c, b, x, y, z, a, c = 100;
    ^
main.cpp:9:8: warning: expression result unused [-Wunused-value]
    c, b, x, y, z, a, c = 100;
       ^
main.cpp:9:11: warning: expression result unused [-Wunused-value]
    c, b, x, y, z, a, c = 100;
          ^
main.cpp:9:14: warning: expression result unused [-Wunused-value]
    c, b, x, y, z, a, c = 100;
             ^
main.cpp:9:17: warning: expression result unused [-Wunused-value]
    c, b, x, y, z, a, c = 100;
                ^
main.cpp:9:20: warning: expression result unused [-Wunused-value]
    c, b, x, y, z, a, c = 100;
                   ^
main.cpp:10:5: warning: variable 'x' is uninitialized when used here [-Wuninitialized]
    x += 5;
    ^
main.cpp:7:16: note: initialize the variable 'x' to silence this warning
    double b, x, y, z, a, c;
               ^
                = 0.0
main.cpp:11:5: warning: variable 'y' is uninitialized when used here [-Wuninitialized]
    y -= 2;
    ^
main.cpp:7:19: note: initialize the variable 'y' to silence this warning
    double b, x, y, z, a, c;
                  ^
                   = 0.0
main.cpp:12:5: warning: variable 'z' is uninitialized when used here [-Wuninitialized]
    z *= 10;
    ^
main.cpp:7:22: note: initialize the variable 'z' to silence this warning
    double b, x, y, z, a, c;
                     ^
                      = 0.0
main.cpp:13:5: warning: variable 'a' is uninitialized when used here [-Wuninitialized]
    a /= b;
    ^
main.cpp:7:25: note: initialize the variable 'a' to silence this warning
    double b, x, y, z, a, c;
                        ^
                         = 0.0
main.cpp:13:10: warning: variable 'b' is uninitialized when used here [-Wuninitialized]
    a /= b;
         ^
main.cpp:7:13: note: initialize the variable 'b' to silence this warning
    double b, x, y, z, a, c;
            ^
             = 0.0
11 warnings generated.
Run Code Online (Sandbox Code Playgroud)