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 the 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
What exactly is the problem with the code containing modulus operator?
How can I fix this?
I am using Code::Blocks version 17.12 with GNU GCC as my compiler.