mercredi 3 juillet 2019

Problems in compiling code due to Modulus operator

I have written a program to store a number (which is predefined by the programmer) in form of digits in an array.

For example, if I want to store a number 1234 in array arrx[4], then its elements would be :

arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4

I try to achieve this using below piece of code :

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
   int arrx[4];    //stores the individual digits of number as array
   int digx = 4;   //total number of digits in number
   int i;
   long int dupx = 1234;  //number which has to be stored in array
   for(i = digx-1; i >= 0 ; i--)
 {
        arrx[digx-i-1] = int(dupx/pow(10,i));
        dupx = dupx%(pow(10,i));
 }
    return 0;
}

However, when I try to compile the above code, I get the following error message :

error: invalid operands of types 'long int' and 'double' to binary 'operator%'

The only conclusion which I was able to draw from above error was that the problem is with the modulus operator.

Therefore, I have following questions in my mind

1) What exactly is the problem with the code containing modulus operator ?

2) How can I fix this ?

I am using CodeBlocks version 17.12 with GNU GCC as my compiler.

Aucun commentaire:

Enregistrer un commentaire