1

I am facing a weird problem in my c program. After getting the float input (salary) from the user, the program ends and skips the lines where it is asking for the integer input (empcode). Can anyone tell me why this is happening and what should I do?

#include <stdio.h>
#include <string.h>

struct employee{
    int empcode;
    float salary;
    char name[30];
};

int main(){
    struct employee e1, e2, e3;

    printf("\nEnter name for e1: ");
    gets(e1.name);
    printf("\nEnter salary for e1: ");
    scanf("%f", e1.salary);
    printf("\nEnter employee code for e1: ");
    scanf("%d", e1.empcode);
    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/q/1694036/995714) – phuclv Jun 14 '21 at 09:09
  • 1
    How do you know that `scanf` does not work? You do not check the return value at all. – Gerhardh Jun 14 '21 at 09:20

1 Answers1

1

You have to write

scanf("%f", &e1.salary);
scanf("%d", &e1.empcode);

instead of

scanf("%f", e1.salary);
scanf("%d", e1.empcode);

That is you need to pass the arguments by references through pointers to them.

Pay attention to that the function gets is unsafe and is not supported by the C Standard. Instead use the function fgets.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335