0

Error: expression must have integer or unscoped enum type

I want to split the input integer into individual characters, but this error occurs when I use the pow() function. I want to know why the error is reported, and how to solve it.

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    int input, onecopy, twocopy;
    cin >> input;
    onecopy = input;
    twocopy = input;
    int n = 0;
    int length = 0;
    /* 记录数字长度n */
    do
    {
        n++;
        onecopy = onecopy / 10;
    } while (!(onecopy / 10 == 0));
    length = n + 1;
    /* 拆数 */
    int num[length];
    int s = n;
    for (int i = length - 1; i = 1; i--)
    {
        num[i] = twocopy % pow(10,s) ;
        twocopy = twocopy / 10;
        s--;
    }

    for (int i = 0; i < n; i++)
    {
        cout << num[i];
    }
}
Remy Lebeau
  • 505,946
  • 29
  • 409
  • 696
  • 6
    That error isn't about `pow`, but about `%`, which is only defined for integers. Add a cast or use `fmod`. – o11c Dec 03 '21 at 03:08
  • 2
    Also, are you sure you want `i = 1` in that loop? – Matt Dec 03 '21 at 03:10
  • 2
    You are using the return from `pow()` as an operand of the `%` operator. `pow()` from `` returns a floating point type. The operands of `%` can only be an integral type. – Peter Dec 03 '21 at 03:13
  • 2
    Also, variable length arrays (`int num[length]`) are not standard C++ – jkb Dec 03 '21 at 03:14
  • If you want to add a custom integer pow function, take a look [here](https://stackoverflow.com/questions/1505675/power-of-an-integer-in-c) – Tyler V Dec 03 '21 at 03:14
  • Thank you everyone, I understand, I have corrected the mistake – 大爱中国 Dec 03 '21 at 03:19
  • If you know your number is positive then int n = log10(onecopy) + 1 ? – DS_London Dec 03 '21 at 07:18

0 Answers0