+ 2

State Error C2297 '%': illegal, right operand has type 'double'

this is a part of a program which is used to turn numbers into digits and store them in an array -num[]- but an error keeps occurring . int v=1204 ; // v is a test subject . int num[10]; int a=1;//a is a variable used to count. num[0]=0; num[a] = v / pow(x, N); for (int n=N; -n < n;n--) {//N is the number of digits after the first one . num[a++] = (v%pow(x, n)) / ((pow(x, n) / x));// pow() is a sub function which calculates power and x is a constant = 10 }

23rd Mar 2018, 12:20 PM
Hossin Amin
Hossin Amin - avatar
13 Antworten
+ 4
It seems your error was triggered by an attempt to pass a double type value (returned by pow() function) to modulo operator, if you cast the returned value from pow() function into int you can have the code to run. In the following I am assuming you already know beforehand the number of digits in the 'v' variable, and I used it as value of 'N'. (Edit) The digits here are placed into the array starting from index zero (value of a) #include <iostream> #include <cmath> using namespace std; int main() { int x = 10, N=4;//<-- N = number of digits int v=1204 ; // v is a test subject . int num[10] = {0,0,0,0,0,0,0,0,0,0}; int a=0;//a is a variable used to count. for (int n=N; -n < n;n--) { num[a++] = (v%(int)pow(x, n)) / (((int)pow(x, n) / x)); } // dump array for(int f:num) cout<<f<<" "; return 0; } Hth, cmiiw
23rd Mar 2018, 1:15 PM
Ipang
+ 6
Alright, I see you have changed the function.
23rd Mar 2018, 2:15 PM
Ipang
+ 5
@Hossin, this problem you are referring to, is with the main code, or the pow function? can you assemble a code link to test with? it's kinda awkward to post snippets back and forth : )
23rd Mar 2018, 4:30 PM
Ipang
+ 5
@Hossin, please set the code public so we can discuss in the code comments. And also what is the meaning of number of digits after the first one (input of N)?
23rd Mar 2018, 4:49 PM
Ipang
+ 4
Can you explain what this code is doing and what's the goal? I am not understanding what you are trying to achieve through this code : )
23rd Mar 2018, 12:34 PM
Ipang
+ 3
Hmm..it's odd that you get that error, the message explained that right hand operand of % is of type double, considering you're using your custom pow function which returns int, I guess the #include <cmath> isn't necessary after all then. P.S. Can you explain to me how the calculation works in the main loop? I'm still digging into it : )
23rd Mar 2018, 1:38 PM
Ipang
+ 2
hmm...... it returned 0 and 1000 (edit) remove if and else statments.
23rd Mar 2018, 2:05 PM
Hossin Amin
Hossin Amin - avatar
+ 1
if the user entered 1204 the digits are separated as an element in num [ ] for example num [1]=1 num [2]=2 num [3]= 0 num [4]=4
23rd Mar 2018, 12:38 PM
Hossin Amin
Hossin Amin - avatar
+ 1
this is my sub function for the power. @Ipang (edit) int pow(int base, int power) { int result = 1; for (int x = 0;x < power;x++) { result = result * base; } return result; }
23rd Mar 2018, 1:25 PM
Hossin Amin
Hossin Amin - avatar
+ 1
i changed it now in the discussion and profile
23rd Mar 2018, 2:11 PM
Hossin Amin
Hossin Amin - avatar
+ 1
but i still have a problem with the big one try running it
23rd Mar 2018, 2:15 PM
Hossin Amin
Hossin Amin - avatar
23rd Mar 2018, 4:40 PM
Hossin Amin
Hossin Amin - avatar
0
done
23rd Mar 2018, 4:49 PM
Hossin Amin
Hossin Amin - avatar