0

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);
}
  • Did you try debugging the code? – Rohan Bari Jul 05 '20 at 04:41
  • `&e.empID[9]` is a pointer to the tenth character in `empID`, the `scanf` function expext a pointer to the *first*. Just about any book, tutorial or class should have taught you that. – Some programmer dude Jul 05 '20 at 04:42
  • Yes, many times. I can't figure what's the issue because if I change the line of code below that I mentioned: " printf("\nEnter the employee's ID: "); scanf("%8d", &e.empID[9]);" to printf("\nEnter the employee's ID: " scanf("%8d", e.empID); it prints back the actual salary. However, it prints random numbers for the employee's ID. I'm wondering why when changing the information in the scanf of employeeID it alters the display of the salary as well. And if I leave the EmployeeID like that it prints it back but 0.00 for the salary. – DD ArtCustom Jul 05 '20 at 04:46
  • I'm a bit new to this. What would you suggest? – DD ArtCustom Jul 05 '20 at 04:50
  • Thanks for the suggestion. I'll include but what about the salary not displaying? – DD ArtCustom Jul 05 '20 at 05:06
  • 1
    On another note, please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). And please don't forget the *minimal* part of your [mcve]. – Some programmer dude Jul 05 '20 at 05:07
  • @DDArtCustom The program is so overwhelming and odd. Tell us what's your purpose? Do you want to store information of several employees inside a `.txt` file? – Shubham Jul 05 '20 at 05:16
  • As you stated your problem, the problem isn't in this line of code: `printf("\nEnter basic salary of the employee: "); scanf("%f", &e.slry);`. It is somewhere else in the program. – Shubham Jul 05 '20 at 05:19
  • I'll suggest you to get rid of this buggy code and try a simple `linear linked list`. Which will make your life easier. – Shubham Jul 05 '20 at 05:22
  • You have `scanf("%s%c%s...")`, the `%c` will eat the **first whitespace character following that which was matched by **`%s`**. You probably mean `"%s %c%s...."` Possibly a duplicate of https://stackoverflow.com/questions/36504135/whitespace-before-c-specification-in-the-format-specifier-of-scanf-function-in – Antti Haapala -- Слава Україні Jul 05 '20 at 06:29
  • It's an employee management system which basically stores, lists, updates and deletes employees' information. – DD ArtCustom Jul 05 '20 at 16:37
  • 1
    @Lucas Thanks for the suggestion but I already came a far way with the code. – DD ArtCustom Jul 05 '20 at 16:53
  • If it's and employee management system then its way better to use `linked_lists`, `pointer` doesn't bother you that much. @DDArtCustom – Shubham Jul 05 '20 at 17:49
  • I have an answer/suggestion for your `employee management system`. You just need to modify it a little bit and you can use it in your program, easily. Follow this [link to the question](https://stackoverflow.com/a/62729120/12180605). – Shubham Jul 05 '20 at 17:52

0 Answers0