While running this code I get an unwanted display: "the information for the salary is not displaying although I inputted the information" This problem most likely exist in line 131 in the insert section (). If I enter "900" for salary it displays "0.00." I also think it's related to the line of code just below it since the display of data changes back to normal however the data for the employee ID changes and prints the address. I ask if you know what's the issue if you could please intervene? Thank you in advance!!
struct here:
struct employee
{
char name[50];
char sex[7];
char adrs[50];
char dsgn[25];
int age;
int empID[9];
float slry;
};
Problem:
printf("\nEnter basic salary of the employee: ");
scanf("%f", &e.slry);
printf("\nEnter the employee's ID: ");
scanf("%8d", &e.empID[9]);
Entire code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <stdbool.h>
#include <windows.h>
#include "struct.h"
void insert();
void list();
void edit();
void del();
void exit();
int tolower();
FILE * fptr, *ftemp;
struct employee e;
size_t recsize;
char empname[50];
int main()
{
int choice;
fptr = fopen("ems.txt", "r+");
if (fptr == NULL)
{
printf("Can't find file! Attempting to create file... \n");
fptr = fopen("ems.txt","w+");
if(fptr == NULL)
{
printf("Can't create file. Exiting...");
exit(1);
}
}
//Explain the reason for this?
recsize = (long int) sizeof(e);
while(1)
{
printf("*******************************\n");
printf("\nEmployee management system");
printf("\n1. Insert employee information");
printf("\n2. List all employee information");
printf("\n3. Edit employee information");
printf("\n4. Delete employee information");
printf("\n5. Exit");
printf("\n\n*****************************\n");
printf("\n\n Enter your choice: ");
scanf("%d", &choice);
int ch; while( ( ch = getchar() ) != EOF && ch != '\n' ){;}
switch(choice)
{
case 1:
puts("Insert was chosen");
insert();
break;
case 2:
puts("List was chosen");
list();
break;
case 3:
puts("Edit was chosen");
edit();
break;
case 4:
puts("Delete was chosen");
del();
break;
case 5:
puts("Exit was chosen");
exit(1);
break;
default:
puts("Choice is incorrect!!");
break;
}
}
return 0;
}
void insert()
{
char next;
do
{
printf("********************************************************** \n");
printf("\nEnter the name of the employee: ");
fgets(e.name, sizeof(e.name), stdin);
e.name[strcspn(e.name, "\n\r")] = '\0';
printf("\nEnter the sex of the employee (M/m or F/f): ");
scanf("%6s",e.sex);
switch(*e.sex)
{
case 'M':
case 'm':
break;
case 'F':
case 'f':
break;
default:
printf("Unspecified Sex.");
}
printf("\nEnter the address of the employee: ");
fseek(stdin, 0, SEEK_END); // ADD THIS TO AVOID SKIP
fgets(e.adrs, sizeof(e.adrs), stdin); // this
e.adrs[strcspn(e.adrs, "\n\r")] = '\0';
printf("\nEnter designation of the employee: ");
fgets(e.dsgn, sizeof(e.dsgn), stdin); // this
e.dsgn[strcspn(e.dsgn, "\n\r")] = '\0';
printf("\nEnter age of the employee: ");
scanf("%d", &e.age);
printf("\nEnter basic salary of the employee: ");
scanf("%f", &e.slry);
printf("\nEnter the employee's ID: ");
scanf("%8d", &e.empID[9]);
fwrite(&e,recsize,1,fptr);
int ch; while( ( ch = getchar() ) != EOF && ch != '\n' ){;}
printf("\nDo you want to input more? (y/n): ");
next = getche();
printf("\n");
}
while( tolower(next) != 'n' );
//fclose(fptr);
}
void list ()
{
/*printf("-------------------------------");
printf("\nEmployee Details: \n---------------------------------\n");
rewind(fptr);///moves file to start of the file
size_t fread(void *fptr, size_t size, size_t nmemb, FILE *stream)///read the file and fetch the record one record per fetch
{
printf("\n\n%s \t\t%6s \t%s \t%s \t%d \t%.2f \t%d",e.name, e.sex, e.adrs, e.dsgn, e.age, e.slry, e.empID[9]);
return 1;
}
getch();*/
printf("-------------------------------");
printf("\nEmployee Details: \n---------------------------------\n");
rewind(fptr);
/*size_t fread(void *fptr, size_t size, size_t nmemb, FILE *stream)///read the file and fetch the record one record per fetch
{*/
printf("Name : %s\n",e.name);
printf("Address : %s\n",e.adrs);
printf("Sex : %7s\n",e.sex);
printf("Designation : %s\n",e.dsgn);
printf("Age : %d\n",e.age);
printf("Salary : %.2f\n",e.slry);
printf("Employee-ID : %d\n",e.empID[9]);
/*return 1;
}*/
}
void edit ()
{
char next;
do
{
printf("Enter the employee's name to be edited: ");
scanf("%49[^\n]", empname);
rewind(fptr);
size_t fread(void *fptr, size_t size, size_t nmemb, FILE *stream)///fetch all records from file
{
if(strcmp(e.name,empname) == 0) ///if entered name matches with that in file
{
printf("\nEnter new name, sex, address, designation, age, salary and employee ID: ");
scanf("%s%c%s%s%d%f%d", e.name, e.sex, e.adrs, e.dsgn, &e.age, &e.slry, e.empID);
fseek(fptr, -recsize, SEEK_CUR);/// move cursor 1 step back from current position
fwrite(&e, recsize,1,fptr); ///override the record
}
return 1;}
printf("\nEdit another record(y/n)");
next = getche();
int ch; while( ( ch = getchar() ) != EOF && ch != '\n' ){;}
}
while(next != 'n');
return ;
}
void del()
{
char next;
do
{
printf("\nEnter name of employee to delete: ");
scanf("%s",empname);
ftemp = fopen("Temp.dat","wb"); ///create a intermediate file for temporary storage
rewind(fptr); ///move record to starting of file
size_t fread(void *fptr, size_t size, size_t nmemb, FILE *stream) ///read all records from file
{
if(strcmp(e.name,empname) != 0) ///if the entered record match
{
fwrite(&e,recsize,1,ftemp); ///move all records except the one which is to be deleted to temp file
}
return 1;
}
fclose(fptr);
fclose(ftemp);
remove("ems.txt"); ///remove original file
rename("Temp.dat","ems.txt"); ///rename temp file to original file name
fptr = fopen("ems.txt", "rb+");
printf("Delete another record(y/n)");
int ch; while( ( ch = getchar() ) != EOF && ch != '\n' ){;}
next = getche();
}while( tolower(next) != 'n' );
fclose(fptr);
exit(0);
}