简体   繁体   中英

C++ / Error: expression must have integral or unscoped enum type

I have the error for my code, I don't know how can fix this, anyone can help me?

Error: expression must have integral or unscoped enum type

I see this error in line 12 on arr[i] and 2.0!

I use visual studio 2019

int n,i,ev=0;
float arr[150];

cin >> n;

for (i = 0; i < n; i++)
{
    cin >> arr[i];
}
for (i = 0; i < n; i++)
{
    if (arr[i] % 2.0 == 0.0)
        ev++;
}

It's what it says: the expression must have integral or unscoped type.

The line and column number points you to the % operation. This is an integer modulo operation , which requires integers .

2.0 is not an integer, and neither is arr[i] .

Use std::fmod instead.

But you're going to struggle here. Finding "even" floating-point numbers may be tricky, because floating-point numbers can be inexact. Decide whether you really need floats and, if you do, decide whether you really need to find out whether they're even whole numbers.

if doing work with integer number is not a problem for you than you can try this code-

int n,i,ev=0;
int arr[150];

cin >> n;

for (i = 0; i < n; i++)
{
    cin >> arr[i];
}
for (i = 0; i < n; i++)
{
    if (arr[i] % 2 == 0)
        ev++;
}

The reason you are having the error is that modular operation with floating-point numbers is not supported. Please let me know if you have further query :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM