2

im programming on c++ and i have this errors for that code:

#include <stdio.h>

int main(int argc, char* argv[])
{ int x;
    printf("%d","Please enter a number\n");
    scanf(%d,&x);
    printf("%d","You entered 56\n");

}

This is the errors: Error 1 error C2065: 'd' : undeclared identifier 9 1 ניסיון1

2   IntelliSense: expected an expression    9   8   

Thanks, Peleg

  • This looks more C than C++ – Caesar Mar 27 '13 at 13:23
  • `printf("%d","You entered 56\n");` is not right either. You need `printf("You entered %d\n", x);`. – john Mar 27 '13 at 13:24
  • Neither is `printf("%d","Please enter a number\n");` you need `printf("Please enter a number\n");`. You might of seen lots of examples using %d but that doesn't mean you have to put %d in front of everything. Reread your books and try to understand what %d means. – john Mar 27 '13 at 13:26
  • possible duplicate of [What is an 'undeclared identifier' error and how do I fix it?](http://stackoverflow.com/questions/22197030/what-is-an-undeclared-identifier-error-and-how-do-i-fix-it) – sashoalm Mar 05 '14 at 13:24

4 Answers4

1

The first argument to scanf should be a null-terminated string:

scanf("%d",&x);

Just as you have done with printf.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
1
scanf(%d,&x);  
------^^---- 

Should be scanf("%d",&x);

Denis Ermolin
  • 5,530
  • 6
  • 27
  • 44
0

As the others already stated, scanf requires a format string, so you must write

scanf("%d", &x);

Also your usage of printf will not produce the results you want. The first string passed to printf is the output format string. "%d" means that the next argument is an integer. Your next argument is the address of a string. What you really wanted to write is one of These three lines:

printf("%s", "Please enter a number\n");
printf("Please enter a number\n");
puts("Please enter a number");

The last line is best in your case. The second line is fine, too, but only because the string does not contain formatting characters like %d.

Werner Henze
  • 16,404
  • 12
  • 44
  • 69
0

The problem is in the first argument of your scanf(%d,&x); statement. This argument should be a null terminated string. Your code is rewritten below:

#include <stdio.h>

int main(int argc, char* argv[])
{ 
    int x;
    printf("Please enter a number %d\n");
    scanf("%d",&x);
    printf("You entered %d\n", x);

}
Bishwa
  • 1
  • 1
  • I didn't actually understood what you mean "argument" and what is it null terminated string (I know what is string but not that kind". Also, now i have another error: Error 1 error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 10 1 – Peleg Megides Mar 27 '13 at 14:32