The program is supposed to do integer division and show the remainder. It should check the if the denominator is 0 and allow the user to correct it. However, when the denominator is 0 and a non-zero integer is re-entered, I get a segmentation fault. Could someone explain why and how I can rectify it. I also get a number of warnings. I am kind of new to using pointers.
Here is my code (C):
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[])
{
int n, d, den, rem;
int *dvd = &den;
int *rmn = &rem;
printf("Enter the numerator: "); //prompt a user to enter an integer numerator
scanf("%i", &n);
printf("%i\n", n);
printf("Enter the denominator: ");//prompt the user to enter an integer denominator
scanf("%i", &d);
printf("%i\n", d);
int division (int numerator, int denominator, int *dividend, int *remainder) {
while (denominator==0) {
printf("Number cannot be 0!\n");
printf("Enter another number ");
scanf("%i", denominator);
}
dividend = numerator/denominator;
remainder = numerator%denominator;
printf("%i/%i = %i with %i remainder\n",n, d, dividend, remainder);
}
division(n, d, *dvd, *rmn); // call the function division
return 0;
}
I have tried using the address of the denominator instead of the pointer which did not work. I also tried nesting the while loop in an if function.