C Program For Employee Menu Driven

C Program For Employee Menu Driven

Here is a sample C program that demonstrates how to create a menu-driven application that prompts the user to enter the details of five employees and stores them in an array list. The program will keep looping until the user enters “quit”. The program uses a while loop to implement this functionality.




#include <stdio.h>
#include <string.h>

#define MAX_EMPLOYEES 5

// Define a struct to represent an employee
typedef struct {
    char name[100];
    int age;
    float salary;
} Employee;

int main(void) {
    // Create an array to hold the employee records
    Employee employees[MAX_EMPLOYEES] = { 0 }; // Initialize the array to all zeros to ensure it is properly initialized

    // Loop until the user enters "quit"
    char input[100] = { 0 }; // Initialize the input string to all zeros to ensure it is zero-terminated
    int num_employees = 0;
    while (num_employees < MAX_EMPLOYEES) {
        printf("Enter the name of employee %d (or 'quit' to exit): ", num_employees + 1);
        if (scanf_s("%99s", input, sizeof(input)) != 1) { // Use scanf_s to read the input string and check the return value to ensure that it was successful
            printf("Error reading input\n");
            return 1;
        }

        if (strcmp(input, "quit") == 0) {
            break;
        }

        // Store the employee's name
        strcpy_s(employees[num_employees].name, sizeof(employees[num_employees].name), input); // Use strcpy_s to copy the input string to the employees array

        // Prompt for and store the employee's age
        printf("Enter the age of employee %d: ", num_employees + 1);
        if (scanf_s("%d", &employees[num_employees].age) != 1) { // Use scanf_s to read the age and check the return value to ensure that it was successful
            printf("Error reading input\n");
            continue; // If the age input was invalid, skip the rest of the current iteration and move on to the next one
        }

        // Prompt for and store the employee's salary
        printf("Enter the salary of employee %d: ", num_employees + 1);
        if (scanf_s("%f", &employees[num_employees].salary) != 1) { // Use scanf_s to read the salary and check the return value to ensure that it was successful
            printf("Error reading input\n");
            continue; // If the salary input was invalid, skip the rest of the current iteration and move on to the next one
        }

        num_employees++;
    }

    // Print the employee records
    printf("\nEmployee records:\n");

    for (int i = 0; i < num_employees; i++) {
        printf("Name: %s\n", employees[i].name);
        printf("Age: %d\n", employees[i].age);
        printf("Salary: %.2f\n", employees[i].salary);
    }

    return 0;
}

Here is a more detailed explanation of the code:

  1. The program starts by defining a struct called Employee to represent an employee record. The struct has three fields: name, age, and salary. These fields will store the employee’s name, age, and salary, respectively.
  2. Next, the program defines a constant called MAX_EMPLOYEES that specifies the maximum number of employees that can be stored in the array.
  3. In the main function, the program creates an array of Employee structs called employees. It initializes the array to all zeros using an initializer list to ensure that it is properly initialized.
  4. The program then declares a string called input and initializes it to all zeros to ensure that it is zero-terminated. It also declares a variable called num_employees and initializes it to zero.
  5. The program enters a while loop that continues until num_employees is equal to MAX_EMPLOYEES. Inside the loop, it prompts the user to enter the name of the next employee (or “quit” to exit). It uses scanf_s to read the input string and checks the return value to ensure that the input was read successfully.
  6. If the user entered “quit”, the program breaks out of the loop using the break statement. Otherwise, it copies the input string to the name field of the employees array using strcpy_s.
  7. The program then prompts the user to enter the age of the employee and uses scanf_s to read the input. If the input is invalid (i.e., scanf_s returns a value other than 1), it prints an error message and continues to the next iteration of the loop using the continue statement.
  8. Similarly, the program prompts the user to enter the salary of the employee and uses scanf_s to read the input. If the input is invalid, it prints an error message and continues to the next iteration of the loop.
  9. If the input for the employee’s name, age, and salary is valid, the program increments num_employees by 1 and moves on to the next iteration of the loop.
  10. Once the loop has finished, the program prints the employee records by iterating through the employees array and printing the values of each field.
READ MORE  C SHARP AND MYSQL DATABASE CRUD TUTORIAL 52 How To clear Or Reset Image On A Picturebox On Button Click

VISUAL STUDIO 2022 OUTPUT

Enter the name of employee 1 (or 'quit' to exit): e
Enter the age of employee 1: 3
Enter the salary of employee 1: 44
Enter the name of employee 2 (or 'quit' to exit): we
Enter the age of employee 2: 4
Enter the salary of employee 2: 555555
Enter the name of employee 3 (or 'quit' to exit): quit

Employee records:
Name: e
Age: 3
Salary: 44.00
Name: we
Age: 4
Salary: 555555.00

D:\MauriceMuteti\C Programming\C Program For Employee Menu Driven\C Program For Employee Menu Driven\x64\Debug\C Program For Employee Menu Driven.exe (process 26272) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .


You can Clone The Code From This Github Repository Link – https://github.com/mauricemuteti/C-Program-For-Employee-Menu-Driven

Leave a Reply

Your email address will not be published. Required fields are marked *