3

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?

8
  • To avoid further problems, don't use scanf for user input. Use fgets instead.
    – melpomene
    Jan 13, 2017 at 20:58
  • 2
    Note that C programs are not called scripts; that term is reserved for programming languages without a formal compiler that produces an executable (such as shells, Awk, Perl, Python, …). They're programs, or source files, or something along those lines. Jan 13, 2017 at 22:56
  • when not using the parameters to main(), then use the signature: int main( void ) Jan 14, 2017 at 9:24
  • the function: scanf() requires the address of its' parameters, so it knows where to place the converted input. so this line: scanf("%d", Integer); should be: scanf("%d", &Integer); Also, always check the returned value to assure the operation was successful. I.E. `if( 1!=scanf( "%d", &Integer ) ) { perror( "scanf failed" ); exit( EXIT_FAILURE ); } Note: exit() and EXIT_FAILURE found in stdlib.h Jan 14, 2017 at 9:27
  • the function: printf() expects the first parameter to be a format string so suggest: printf( "%d %d %d", DIgit3, Digit1, Digit2 ); Jan 14, 2017 at 9:31

3 Answers 3

5

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?

3
  • 1
    Compiler flags for optimization, but none to enable warnings? Jan 13, 2017 at 21:36
  • 1
    I only meant that, as long as you are showing a compiler invocation, it might be nice to show those new to C at least -Wall -Wextra, which will help them catch a lot of errors. I personally use these in addition to -pedantic almost every time I invoke gcc. Jan 13, 2017 at 21:49
  • 1
    You're correct that the code runs properly, however it's not yielding the same result as what I should be getting. Is there an error in my math or in my understanding of the question? Because 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?
    – user6093969
    Jan 14, 2017 at 5:55
2

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.

0

Welcome to C, hopefully you will enjoy it.

It seems that you forgot a &. Please do scanf("%d", &Integer) instead of scanf("%d", Integer). In C most variables are referenced as pointers, so when you wish to assign an integer or a float by scanf, you have to use a "&" which will deference it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.