I am supposed to ask the user for a 3 digit number and then replaces each digit by that digit plus 6 modulus 10, then sum all update digits. When I run the program it crashes after I enter a number, warning:
format %d expects argument of type 'int *' but argument 2 has type int.
Here is my source code:
#include <stdio.h>
int main(int argc, char* argv[])
{
int Integer;
int Divider;
int Digit1, Digit2, Digit3;
printf("Enter a three-digit integer: ");
scanf("%d", Integer);
Divider = 1000;
Digit1 = Integer / Divider;
Integer = Integer % Divider;
Divider = 100;
Digit2 = Integer / Divider;
Integer = Integer % Divider;
Divider = 10;
Digit3 = Integer / Divider;
Digit1 = (Digit1 + 6) % 10;
Digit2 = (Digit2 + 6) % 10;
Digit3 = (Digit3 + 6) % 10;
printf(Digit3 + Digit1 + Digit2);
getch();
return 0;
}
Update
In our example output if the user enters 928 the resulting number should be 584. I'm not exactly sure how that number is made. It should replace each digit by the sum of that digit plus 6 modulus 10. So is there a math error in my code as well?
Several places:
scanf("%d", &Integer);
printf("%d\n", Digit3 + Digit1 + Digit2);
getchar(); // maybe??
In fact, compiler points them out clearly for you:
warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%d", Integer);
^
warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [enabled by default]
printf(Digit3 + Digit1 + Digit2);
^
warning: implicit declaration of function ‘getch’ [-Wimplicit-function-declaration]
getch();
^
Note, regarding getch, you can read further here: implicit declaration of function 'getch'.
After these fixes, your program runs without error. Let me call the fixed source file z.c,
gcc -std=gnu99 -O2 -o z z.c
./z
If I input 304, I get 21.
Additional note:
Do you want Divider to be 100, 10, 1, rather than 1000, 100, 10?
When you use scanf to read values into variables, you need to pass the addresses of the variables , so that scanf can change the values of the variables.
scanf("%d", &Integer);
The & sign passes the address of the variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With