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;
}
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
.