On the line that says 'mod = number%iter;', I can replace the modulus with any other operation and it works, but not with modulus. Does anyone know why this is? It just does printf the first two times and then says
RUN FAILED (exit value: 1, total time: 1s)
There will be more code to come, so the function might me useless right now but it won't be in the future. Remember that I'm completely new so please don't over complicate it for my little brain! Here is the code:
#include <stdio.h>
#include <string.h>
int test(int number) {
printf("Number = %d\n",number);
int iter;
int mod;
for (iter=0;iter<number;iter+=1) {
printf("%d ",iter);
printf("%d ",number);
mod = number%iter;
printf("%d\n",mod);
}
return 0;
}
int main(){
printf("C never works\n");
test(10);
}
Using the modulus operator gives you the remainder after it divides the left-hand operand by right-hand operand. Your loop starts at iter = 0, and you do:
mod = number%iter;
Which does division by zero. To use the modulus operator means doing a division, and checking the remainder. On my compiler at least it specifies what the exception is.
Unhandled exception at -------- Integer division by zero
Division by zero is undefined not only in programming, but in maths too. You can't divide by zero. The comedian Steven Wright said "Black holes are where God divided by zero." So adjust your code accordingly.