-3

I wrote this code that takes user input and then prints it out, but it's not working properly. It only recieves the name but not the age or salary of worker.

 #include <stdio.h>
 #include <stdlib.h>
struct worker
{
 char sri[100];
 int age;
 double salary;
  };
int main()
{
  struct worker *ptr;
 int n;
 printf("enter number of employy  ");
 scanf("%d", &n);
 //allocating memory for n ;
 ptr = (struct worker *)malloc(n * sizeof(struct worker));

 for (int i = 0; i < n; ++i)
 {

printf("For employee: %d \n ", i+1);
printf("Enter your name : ");
scanf("%s\n", (ptr + i)->sri);
printf("Enter salary:\n ");
scanf("%lf \n", (ptr + i)->salary);
printf("Enter age : \n");
scanf("%d \n", (ptr + i)->age);

 }

 
for (int i = 0; i < n; ++i)
{
 printf("Employee %d: ", i+1);
  printf("name =%s ", (ptr + i)->sri);
  printf("age = %d, ", (ptr + i)->age);
  printf("salary = %.2lf\n", (ptr + i)->salary);
 }
 free(ptr);

 return 0;
 }
SuperStormer
  • 4,997
  • 5
  • 25
  • 35
  • https://ericlippert.com/2014/03/05/how-to-debug-small-programs/, [ask] – SuperStormer Sep 04 '21 at 04:34
  • I check Code is working. I mean there are no errors. What are you trying to do ? And what problem you are facing ? – Ashok Sep 04 '21 at 04:40
  • 2
    `scanf` expects a pointer for its arguments. The argument is not a pointer here: `scanf("%lf \n", (ptr + i)->salary);` or here: `scanf("%d \n", (ptr + i)->age);`. When that is corrected it seems to work fine: https://ideone.com/TP4khd – Retired Ninja Sep 04 '21 at 04:45
  • 1
    OT: Change `(ptr + i)->` to `ptr[i].` to increase readability – Support Ukraine Sep 04 '21 at 05:47
  • Putting a space or `\n` after the `%s` or `%d` in `scanf` is valid but does a weird thing you almost certainly don't want; either omit it altogether, or put it at the front, like `scanf(" %s", ...)` – Jiří Baum Sep 04 '21 at 06:08
  • Using `%s` in `scanf` without a limit is a major security hole; always put a limit on it, like `scanf("%99s", ...)`, regardless of whether it has any security implications in the current context – Jiří Baum Sep 04 '21 at 06:13
  • can you give me right code of this ?Just remove the error .Put your whole code – raz Sudhanshu Sep 04 '21 at 06:38

1 Answers1

1

There are several things to say about your program but let's start with the part that makes it fail - these two lines:

scanf("%lf \n", (ptr + i)->salary);
scanf("%d \n", (ptr + i)->age);

(ptr + i)->salary is a double but here scanf requires a pointer to double

(ptr + i)->age is an int but here scanf requires a pointer to int

If you didn't get compiler warnings for this, you really need to increase the compilers warning level. For instance for gcc you should at least use -Wall -Werror. In that case you would have been told something like:

main.cpp: In function 'main':
main.cpp:36:10: error: format '%lf' expects argument of type 'double *', but argument 2 has type 'double' [-Werror=format=]
   36 | scanf("%lf \n", (ptr + i)->salary);
      |        ~~^      ~~~~~~~~~~~~~~~~~
      |          |               |
      |          double *        double
main.cpp:38:9: error: format '%d' expects argument of type 'int *', but argument 2 has type 'int' [-Werror=format=]
   38 | scanf("%d \n", (ptr + i)->age);
      |        ~^      ~~~~~~~~~~~~~~
      |         |               |
      |         int *           int
cc1: all warnings being treated as errors

which tells you it all !

Other comments

Always check the value returned by scanf - like:

scanf("%d", &n); --> if (scanf("%d", &n) != 1) { // error handling }

Don't put space or \n in the end of your scanf format string. It will give you unexpected behavior.

When using %s in scanf always set a size limit.

So:

scanf("%s\n", --> scanf("%99s",

Don't cast the value returned by malloc. So instead of

ptr = (struct worker *)malloc(n * sizeof(struct worker));

do

ptr = malloc(n * sizeof(struct worker));

or better

ptr = malloc(n * sizeof *ptr);

For better readability do

(ptr + i)->age --> ptr[i].age

So to get back to your original problem.

Instead of:

scanf("%d \n", (ptr + i)->age);

do

if (scanf("%d", &ptr[i].age) != 1) { exit(1); }
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63