C++ Employee Array Menu Driven Program

C++ Employee Array Menu Driven Program

Here is an example of a C++ menu-driven application that prompts the user to enter the details of five employees and stores them in an array list. The program will continue to loop until the user enters “quit”.





// C++ Employee Array Menu Driven Program.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <string>

const int NUM_EMPLOYEES = 5;  // number of employees to be entered

// Employee class to store the details of each employee
class Employee {
public:
    std::string name;  // name of the employee
    int age;           // age of the employee
    std::string role;  // role of the employee
};

int main() {
    Employee employees[NUM_EMPLOYEES];  // array to store the employee objects

    // loop until the user enters "quit"
    std::string input;
    int i = 0;  // counter for number of employees entered
    while (input != "quit") {
        std::cout << "Enter the details of employee " << i + 1 << ":" << std::endl;
        std::cout << "Name: ";
        std::cin >> employees[i].name;
        std::cout << "Age: ";
        std::cin >> employees[i].age;
        std::cout << "Role: ";
        std::cin >> employees[i].role;
        std::cout << "Enter 'quit' to exit or any other key to continue: ";
        std::cin >> input;
        i++;
    }

    // print the details of all the employees entered
    std::cout << "The details of the employees are:" << std::endl;
    for (int j = 0; j < i; j++) {
        std::cout << "Employee " << j + 1 << ":" << std::endl;
        std::cout << "Name: " << employees[j].name << std::endl;
        std::cout << "Age: " << employees[j].age << std::endl;
        std::cout << "Role: " << employees[j].role << std::endl;
    }

    return 0;
}


// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

In this program, there is a single class called Employee, which represents an employee with a name, age, and role. An array of Employee objects is used to store the details of the employees.

The program uses a while loop to repeatedly prompt the user for the details of each employee. The loop continues until the user enters “quit”. The details of each employee are stored in an element of the employees array.

After the loop, the details of all the employees are printed using a for loop.

The program begins by including the necessary header files, iostream and string, and defining a constant NUM_EMPLOYEES that determines the number of employees that will be entered.

Next, a class called Employee is defined. This class has three member variables: name, age, and role. These variables represent the name, age, and role of an employee, respectively.

In the main function, an array of Employee objects called employees is declared. This array will be used to store the details of the employees that the user enters.

A while loop is then used to repeatedly prompt the user for the details of each employee. The loop continues until the user enters “quit”. Inside the loop, the user is asked to enter the name, age, and role of the employee. These values are then stored in the corresponding member variables of the Employee object in the employees array.

After the loop, a for loop is used to print the details of all the employees that were entered. The loop iterates through each element of the employees array and prints the values of the name, age, and role member variables

VISUAL STUDIO 2022 OUTPUT

Enter the details of employee 1:
Name: Jane
Age: 22
Role: It
Enter 'quit' to exit or any other key to continue: d
Enter the details of employee 2:
Name: Hellen
Age: 32
Role: Receptionist
Enter 'quit' to exit or any other key to continue: quit
The details of the employees are:
Employee 1:
Name: Jane
Age: 22
Role: It
Employee 2:
Name: Hellen
Age: 32
Role: Receptionist

D:\MauriceMuteti\C++\C++ Employee Array Menu Driven Program\C++ Employee Array Menu Driven Program\x64\Debug\C++ Employee Array Menu Driven Program.exe (process 11408) 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 . . .

Leave a Reply

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