Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: "expression must have integral or unscoped enum type" [duplicate]

So guys I'm new I tried looking up a solution however, I didn't really understand it that well, I'm writing shorthand to understand how they can be replaced with other pieces of code, so I ran across the modulus to which I added however it gives a "expression must have integral or unscoped enum type".

I don't know enum type is exactly, they code doesn't run?

#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;
}

the problem here is that "c" gives the "expression must have integral or unscoped enum type" error.

I know what the modulus does, it gives the remainder of the division between 2 numbers, however I'm stumped in this case because should it gives a remainder? Is it syntactically wrong?

like image 731
Victor Martins Avatar asked Dec 06 '22 16:12

Victor Martins


1 Answers

c is double, thus you cannot use the modulo operator %.

Use fmod() instead.

So change this:

c %= 3

to this:

c = fmod(c, 3);

As Slava mentioned, you could have used an int instead, like this:

int c = 5; // for example
c %= 3

which wouldn't require the use of fmod(). It's important to understand that the modulo operator % works with ints.


As πάντα ρέι mentioned, there is also this: Can't use modulus on doubles?


As a side note Victor, you have so many variables, but most of them are unused and or uninitialized. Did you compile with all the warnings enabled? Here is what I get when compiling your original code (with the line that generates the error commented out):

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.
like image 81
gsamaras Avatar answered Jan 18 '23 23:01

gsamaras