Untitled

 avatar
unknown
plain_text
5 months ago
16 kB
3
Indexable
Here are the required changes to integrate an `Address` structure inside the `Employee` structure:

1. **employee.h**
   - Define the `Address` structure and include it inside `Employee`.

```c
#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <stdio.h>
#include <stdlib.h>

struct Address {
    int houseno;
    char *street;
    char *city;
    int pincode;
};

struct Employee {
    int empid;
    char *empname;      // Pointer for employee name
    int esalary;
    char *designation;  // Pointer for designation
    struct Address address;  // Address of the employee
};

// Declare the employeeCount variable as extern
extern int employeeCount;

void collectEmployeeInfo(struct Employee *emp);
void searchEmployee(int empid);
void modifyEmployee(int empid);
void deleteEmployee(int empid);
void displayAllEmployees();
void saveToFile(struct Employee *emp);

#endif
```

2. **collect.c**
   - Update the `collectEmployeeInfo` function to collect the `Address` details.

```c
void collectEmployeeInfo(struct Employee *emp) {
    emp->empname = malloc(100 * sizeof(char)); // Allocate memory for empname
    emp->designation = malloc(100 * sizeof(char)); // Allocate memory for designation
    emp->address.street = malloc(100 * sizeof(char)); // Allocate memory for street
    emp->address.city = malloc(100 * sizeof(char));   // Allocate memory for city

    if (emp->empname == NULL || emp->designation == NULL || emp->address.street == NULL || emp->address.city == NULL) {
        printf("Memory allocation failed.\n");
        exit(1);
    }

    // Collect Employee ID
    printf("Enter Employee ID (integer): ");
    // Similar validation and duplicate checking as before

    // Collect Employee Name
    printf("Enter Employee Name (alphabetic only): ");
    while (!validateStringInput(emp->empname, 100));

    // Collect Employee Salary
    printf("Enter Employee Salary (integer): ");
    while ((emp->esalary = validateIntegerInput()) == -1);

    // Collect Designation
    printf("Enter Designation (alphabetic only): ");
    while (!validateStringInput(emp->designation, 100));

    // Collect Address
    printf("Enter House Number: ");
    while ((emp->address.houseno = validateIntegerInput()) == -1);

    printf("Enter Street: ");
    while (!validateStringInput(emp->address.street, 100));

    printf("Enter City: ");
    while (!validateStringInput(emp->address.city, 100));

    printf("Enter Pincode: ");
    while ((emp->address.pincode = validateIntegerInput()) == -1);

    employeeCount++; // Increment employee count
}
```

3. **save.c**
   - Save the address details to the file.

```c
void saveToFile(struct Employee *emp) {
    FILE *file = fopen(FILENAME, "a"); // Open in append mode
    if (file) {
        fprintf(file, "%d,%s,%d,%s,%d,%s,%s,%d\n", emp->empid, emp->empname, emp->esalary, emp->designation,
                emp->address.houseno, emp->address.street, emp->address.city, emp->address.pincode);
        fclose(file);
    } else {
        printf("Error opening file for writing.\n");
    }
}
```

4. **display.c**
   - Display the new address fields.

```c
void displayAllEmployees() {
    FILE *file = fopen("employees.txt", "r"); // Open file in read mode
    if (file == NULL) {
        printf("File does not exist.\n");
        return;
    }

    struct Employee emp;
    emp.empname = malloc(100 * sizeof(char)); 
    emp.designation = malloc(100 * sizeof(char));
    emp.address.street = malloc(100 * sizeof(char));
    emp.address.city = malloc(100 * sizeof(char));

    if (emp.empname == NULL || emp.designation == NULL || emp.address.street == NULL || emp.address.city == NULL) {
        printf("Memory allocation failed.\n");
        fclose(file);
        return;
    }

    int recordFound = 0;

    // Print table header
    printf("----------------------------------------------------------------------------------------------------------------------------------\n");
    printf("| %-10s | %-20s | %-10s | %-20s | %-10s | %-20s | %-10s | %-10s |\n", 
           "Emp ID", "Name", "Salary", "Designation", "House No", "Street", "City", "Pincode");
    printf("----------------------------------------------------------------------------------------------------------------------------------\n");

    while (fscanf(file, "%d,%99[^,],%d,%99[^,],%d,%99[^,],%99[^,],%d\n", 
                  &emp.empid, emp.empname, &emp.esalary, emp.designation, 
                  &emp.address.houseno, emp.address.street, emp.address.city, &emp.address.pincode) == 8) {
        recordFound = 1;
        printf("| %-10d | %-20s | %-10d | %-20s | %-10d | %-20s | %-10s | %-10d |\n",
               emp.empid, emp.empname, emp.esalary, emp.designation, emp.address.houseno, emp.address.street, emp.address.city, emp.address.pincode);
    }

    if (!recordFound) {
        printf("| %-10s | %-20s | %-10s | %-20s |\n", "No records", "", "", "");
    }

    printf("----------------------------------------------------------------------------------------------------------------------------------\n");

    free(emp.empname);
    free(emp.designation);
    free(emp.address.street);
    free(emp.address.city);
    fclose(file);
}
```

5. **search.c**
   - Include the address details when displaying a searched employee.

```c
void searchEmployee(int empid) {
    FILE *file = fopen("employees.txt", "r");
    if (file == NULL) {
        printf("File does not exist.\n");
        return;
    }

    struct Employee emp;
    emp.empname = malloc(100 * sizeof(char)); 
    emp.designation = malloc(100 * sizeof(char));
    emp.address.street = malloc(100 * sizeof(char));
    emp.address.city = malloc(100 * sizeof(char));

    if (emp.empname == NULL || emp.designation == NULL || emp.address.street == NULL || emp.address.city == NULL) {
        printf("Memory allocation failed.\n");
        fclose(file);
        return;
    }

    int found = 0;

    while (fscanf(file, "%d,%99[^,],%d,%99[^,],%d,%99[^,],%99[^,],%d\n", 
                  &emp.empid, emp.empname, &emp.esalary, emp.designation, 
                  &emp.address.houseno, emp.address.street, emp.address.city, &emp.address.pincode) == 8) {
        if (emp.empid == empid) {
            printf("Employee ID: %d\n", emp.empid);
            printf("Employee Name: %s\n", emp.empname);
            printf("Employee Salary: %d\n", emp.esalary);
            printf("Designation: %s\n", emp.designation);
            printf("Address: %d, %s, %s, %d\n", emp.address.houseno, emp.address.street, emp.address.city, emp.address.pincode);
            found = 1;
            break;
        }
    }

    if (!found) {
        printf("Employee with ID %d not found.\n", empid);
    }

    free(emp.empname);
    free(emp.designation);
    free(emp.address.street);
    free(emp.address.city);
    fclose(file);
}
```

6. **modify.c**
   - Add the option to modify address details.

```c
void modifyEmployee(int empid) {
    FILE *file = fopen("employees.txt", "r+");
    if (file == NULL) {
        printf("File does not exist.\n");
        return;
    }

    struct Employee emp;
    emp.empname = malloc(100 * sizeof(char)); 
    emp.designation = malloc(100 * sizeof(char));
    emp.address.street = malloc(100 * sizeof(char));
    emp.address.city = malloc(100 * sizeof(char));

    if (emp.empname == NULL || emp.designation == NULL || emp.address.street == NULL || emp.address.city == NULL) {
        printf("Memory allocation failed.\n");
        fclose(file);
        return;
    }

    int recordFound = 0;
    long pos;

    while ((pos = ftell(file)) != -1 && fscanf(file, "%d,%99[^,],%d,%99[^,],%d,%99[^,],%99[^,],%d\n", 
                                               &emp.empid, emp.empname, &emp.esalary, emp.designation, 
                                               &emp.address.houseno, emp.address.street, emp.address.city, &emp.address.pincode) == 8) {
        if (emp.empid == empid) {
            recordFound = 1;
            printf("Employee found.\n");

            while (1) {
                printf("What would you like to modify?\n");
                printf("1. Employee ID\n2. Employee Name\n3. Employee Salary\n4. Designation\n5. Address\n6. Exit\n");
                printf("Enter your choice: ");
                
                int choice;
                scanf("%d", &choice);
                getchar(); 

                switch (choice) {
                    case

 1:
                        printf("Enter new Employee ID: ");
                        emp.empid = validateIntegerInput();
                        break;
                    case 2:
                        printf("Enter new Employee Name: ");
                        validateStringInput(emp.empname, 100);
                        break;
                    case 3:
                        printf("Enter new Salary: ");
                        emp.esalary = validateIntegerInput();
                        break;
                    case 4:
                        printf("Enter new Designation: ");
                        validateStringInput(emp.designation, 100);
                        break;
                    case 5:
                        printf("Enter new Address\n");

                        printf("Enter House Number: ");
                        emp.address.houseno = validateIntegerInput();

                        printf("Enter Street: ");
                        validateStringInput(emp.address.street, 100);

                        printf("Enter City: ");
                        validateStringInput(emp.address.city, 100);

                        printf("Enter Pincode: ");
                        emp.address.pincode = validateIntegerInput();
                        break;
                    case 6:
                        fseek(file, pos, SEEK_SET); 
                        fprintf(file, "%d,%s,%d,%s,%d,%s,%s,%d\n", emp.empid, emp.empname, emp.esalary, emp.designation, 
                                emp.address.houseno, emp.address.street, emp.address.city, emp.address.pincode);
                        fclose(file);
                        printf("Modification complete.\n");
                        return;
                    default:
                        printf("Invalid choice. Try again.\n");
                        break;
                }
            }
        }
    }

    if (!recordFound) {
        printf("Employee with ID %d not found.\n", empid);
    }

    free(emp.empname);
    free(emp.designation);
    free(emp.address.street);
    free(emp.address.city);
    fclose(file);
}
```

These changes should allow you to properly manage the new `Address` structure across all relevant functionality in your program.





You're right. Let's review **main.c** and **delete.c**. In these two files, we'll need minimal changes, primarily related to managing the `Address` structure where applicable. 

1. **main.c**
   - Since `main.c` generally contains the main menu and calls the functions implemented elsewhere, no major changes are needed here, except making sure that the `collectEmployeeInfo`, `modifyEmployee`, `deleteEmployee`, and other functions are correctly called.

Here's what the main menu could look like after adding the `Address` structure, but without other changes:

```c
#include "employee.h"

int main() {
    int choice, empid;
    struct Employee emp;

    while (1) {
        printf("\n--- Employee Management System ---\n");
        printf("1. Add Employee\n");
        printf("2. Search Employee\n");
        printf("3. Modify Employee\n");
        printf("4. Delete Employee\n");
        printf("5. Display All Employees\n");
        printf("6. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                collectEmployeeInfo(&emp);
                saveToFile(&emp);
                break;
            case 2:
                printf("Enter Employee ID to search: ");
                scanf("%d", &empid);
                searchEmployee(empid);
                break;
            case 3:
                printf("Enter Employee ID to modify: ");
                scanf("%d", &empid);
                modifyEmployee(empid);
                break;
            case 4:
                printf("Enter Employee ID to delete: ");
                scanf("%d", &empid);
                deleteEmployee(empid);
                break;
            case 5:
                displayAllEmployees();
                break;
            case 6:
                printf("Exiting...\n");
                exit(0);
            default:
                printf("Invalid choice! Please try again.\n");
        }
    }
    return 0;
}
```

No changes are needed in the logic itself here. This main function ensures the user interacts with the functions that are updated to handle `Address` properly.

2. **delete.c**
   - Here, we'll ensure that the `Address` fields are handled during deletion. While no additional logic is needed for `Address` (since deletion doesn't involve modification of the fields), we must make sure that when an employee is deleted, the record including the address is removed.

Here's the updated `deleteEmployee` function:

```c
void deleteEmployee(int empid) {
    FILE *file = fopen("employees.txt", "r");
    if (file == NULL) {
        printf("File does not exist.\n");
        return;
    }

    FILE *tempFile = fopen("temp.txt", "w");
    if (tempFile == NULL) {
        printf("Error opening temporary file.\n");
        fclose(file);
        return;
    }

    struct Employee emp;
    emp.empname = malloc(100 * sizeof(char));
    emp.designation = malloc(100 * sizeof(char));
    emp.address.street = malloc(100 * sizeof(char));
    emp.address.city = malloc(100 * sizeof(char));

    if (emp.empname == NULL || emp.designation == NULL || emp.address.street == NULL || emp.address.city == NULL) {
        printf("Memory allocation failed.\n");
        fclose(file);
        fclose(tempFile);
        return;
    }

    int found = 0;

    while (fscanf(file, "%d,%99[^,],%d,%99[^,],%d,%99[^,],%99[^,],%d\n", 
                  &emp.empid, emp.empname, &emp.esalary, emp.designation, 
                  &emp.address.houseno, emp.address.street, emp.address.city, &emp.address.pincode) == 8) {
        if (emp.empid != empid) {
            // Write the current employee record to the temp file if it's not the one being deleted
            fprintf(tempFile, "%d,%s,%d,%s,%d,%s,%s,%d\n", emp.empid, emp.empname, emp.esalary, emp.designation,
                    emp.address.houseno, emp.address.street, emp.address.city, emp.address.pincode);
        } else {
            found = 1;
        }
    }

    fclose(file);
    fclose(tempFile);
    
    free(emp.empname);
    free(emp.designation);
    free(emp.address.street);
    free(emp.address.city);

    if (found) {
        remove("employees.txt");    // Delete the original file
        rename("temp.txt", "employees.txt"); // Rename temp file to the original filename
        printf("Employee with ID %d deleted successfully.\n", empid);
        employeeCount--; // Decrement employee count
    } else {
        remove("temp.txt"); // Delete temp file if no matching record was found
        printf("Employee with ID %d not found.\n", empid);
    }
}
```

### Summary:
- **main.c** doesn't need any changes because it simply calls the other functions, which are already updated.
- **delete.c** handles deleting an employee, and we've made sure to include the employee's `Address` details when managing file operations.

With these changes, your program will now handle the `Address` structure properly when adding, modifying, searching, deleting, and displaying employee records.
Editor is loading...
Leave a Comment