0

Something is wrong with the functionality of the code. As in the screenshot below it'skipping storing some information for the employees. Hence it makes it incomplete. I can't fathom what's the underlining issue. This is a screenshot of how it displays: enter image description here

Only the name, sex and address are been stored. Sometimes it randomly stores all information. A look at the code:

        printf("\nEnter designation of the employee: ");
        scanf("%s", e.dsgn);
        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("%d", &e.empID);
        fputs(e.name, fptr);
        fputs(&e.sex, fptr);
        fputs(e.adrs, fptr);
        fputs(e.dsgn, fptr);
        fprintf(fptr, "%d \n%f \n%d \n", e.age, e.slry, e.empID);

The struct:

struct employee
{
        char name[50];
        char sex{7};
        char adrs[50];
        char dsgn[25];
        int age,empID;
        float slry;
        tolower()
};

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();

FILE * fptr, *ftemp;
struct employee e;
long int 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);
        fflush(stdin);

        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);
        printf("\nEnter the sex of the employee (M/m or F/f): ");
        scanf("%c",&e.sex);

        switch(e.sex)
        {
            case 'M':
            case 'm':
                printf("\nMale.\n");
                break;
            case 'F':
            case 'f':
                printf("\nFemale.\n  ");
                break;
            default:
                printf("Unspecified Sex.");
        }
        printf("\nEnter the address of the employee: ");
        scanf( "%s", e.adrs);
        printf("\nEnter designation of the employee: ");
        scanf("%s", e.dsgn);
        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("%d", &e.empID);
        fputs(e.name, fptr);
        fputs(&e.sex, fptr);
        fputs(e.adrs, fptr);
        fputs(e.dsgn, fptr);
        fprintf(fptr, "%d \n%f \n%d \n", e.age, e.slry, e.empID);
       // fwrite(&e,recsize,1,fptr);
        int ch;  while( ( ch = getchar() ) != EOF && ch != '\n' ){;}
        //fflush(stdin);//
        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");
     printf("Name        : %s\n",e.name);
     printf("Address     : %s\n",e.adrs);
     printf("Sex         : %c\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);
}

void edit ()
{
    char next;
    do
    {
        printf("Enter the employee name to be edited: ");
        scanf("%49[^\n]", empname);
        while(fread(&e,recsize,1,fptr)==1)
        {
            if(strcmp(e.name,empname) == 0)
            {
                printf("\nEnter new name,sex,address,designation,age,salary,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);
                fwrite(&e,recsize,1,fptr);
                break;
            }
        }
        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");
        while(fread(&e,recsize,1,fptr) == 1)
        {
            if(strcmp(e.name,empname) != 0)
            {
                fwrite(&e,recsize,1,ftemp);
            }
        }

        fclose(fptr);
        fclose(ftemp);
        remove("ems.txt");
        rename("Temp.dat","ems.txt");
        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' );
}
the busybee
  • 10,755
  • 3
  • 13
  • 30

1 Answers1

0

The scanf("%s", ...) will just take the string value till the next whitespace, rest are truncated. To avoid that, better use fgets(var, sizeof(var), stdin).

printf("\nEnter the address of the employee: ");
scanf( "%s", e.adrs); // this
printf("\nEnter designation of the employee: ");
scanf("%s", e.dsgn); // this

To:

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
printf("\nEnter designation of the employee: ");
fgets(e.dsgn, sizeof(e.dsgn), stdin); // this

Notice that I've mentioned to add a line:

fseek(stdin, 0, SEEK_END);

This will prevent the the skip problem and is better than fflush(stdin) which tends to Undefined Behavior (UB).

Then you will get the correct output format:

Enter the name of the employee: ABC Someone

Enter the sex of the employee (M/m or F/f): m

Male.

Enter the address of the employee: Address 123

Enter designation of the employee: Manager

Enter age of the employee: 120

Enter basic salary of the employee: -10

Enter the employee's ID: 007

Do you want to input more? (y/n): n

An important note: The fseek() will not work on terminals. On a Unix system, if standard input is a terminal, the terminal is not a seek-able device, so the proposed fseek() would do nothing. If the input device was a file, it would, of course, seek to the end of the file.

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34