Home > Enterprise >  inputting a certain number for wages doesn't stop the do while loop when it reaches the number
inputting a certain number for wages doesn't stop the do while loop when it reaches the number

Time:02-14

   #include <stdio.h>

int main()
{
char firstname[20];
char lastname[20]; 
float wage;
int n_emp;
int e_numemp;
FILE *staff_ptr;

staff_ptr=fopen ("staff", "wage");
printf("Input the numbers employees you would like to add\n");
scanf("%d",&n_emp);
do {
  (e_numemp=n_emp-1);
  printf("Enter the first name of the employee\n");
  scanf("%s",&firstname);
  printf("Enter the second name of the employee\n");
  scanf("%s",&lastname);
  printf("Enter the wage of the employee\n");
  scanf("%f",&wage);
  fwrite (&firstname,sizeof (firstname),1,staff_ptr);
  fwrite (&lastname,sizeof (lastname),1,staff_ptr );
  fwrite (&wage,sizeof (wage),1,staff_ptr );
}
while (e_numemp!=0);
fclose (staff_ptr);
return 0;
}

The question for my assignment is "Write a program to ask for the name and wage of each employee in a firm and store the data in a file called "staff"

For some reason when i input a number like 4 for the numbers of employees it just doesn't stop at 4 and keeps going on forever

CodePudding user response:

In every iteration of the loop e_numemp is always set to n_emp - 1 (n_emp is never changed once input). Assuming you wanted to save the initial value of n_emp, perhaps you meant:

scanf("%d",&n_emp);
e_numemp = n_emp;
do {
  e_numemp -= 1;
  .
  .
  .
} while ( e_numemp != 0 );
  •  Tags:  
  • c
  • Related