Question #52326

Write a C++ program that stores the records of 10 employees. The program must display a
menu to enter the employee details and perform the functions given below. The menu must
contain an exit option.

The employees have an hourly rate to calculate the salary based on the amount of hours
worked. The program must store the employee Name, ID, Gender, Age, Hourly rate, Hours.

The employee ID is auto generated an must not be asked from the user
1. Write a function called employeeGender to display the employee name and ID of
all female and male employees based on user input.

2. Write a function called displayAgeRange that accepts an age range and displays
the name and age of the employee that fits the range passed into the function.

3. Write a function called calcSalary that calculates the employee salary based on
the given formula. The function must accept the rate and hours needed to calculate
the salary.

4. Write a function called employeeDetails that displays the full employee details
based on a given emplo
1

Expert's answer

2015-05-05T10:57:24-0400

Answer on Question #52326 – Programming – C++

#include <iostream>
#include <string>
using namespace std;
class Employee {
public:
    Employee() {
        id = 0;
        available = 0;
        age = 0;
        gender = 0;
        hourlyRate = 0.0f;
        hours = 0.0f;
    }
    int available; // 0 - not visible in list of employees, 1 - visible
    int id;
    float hourlyRate;
    float hours;
    string name;
    int gender; // 0 - male, 1 - female
    int age;
    void showDetails() {
        for (int i = 0; i < 79; i++) cout << "*";
        cout << endl;
        cout << "Employee with id " << id << " has name: " << name << endl;
        cout << "he has worked " << hours << " with hourlyRate = " << hourlyRate << endl;
        string temp;
        if (gender) temp = "female";
        else temp = "male";
        cout << "Gender: " << temp << " and Age: " << age;
        cout << endl;
        for (int i = 0; i < 79; i++) cout << "*";
        cout << endl;
    }
    float calculateSalary() {
        float result = hourlyRate * hours;
        return result;
    }
};
int main(int argc, char* argv[]) {
    Employee people[10];
    char input[50];
    int choice;
    int minAge, maxAge;
    Employee *current = 0;
    int id = 0;
    while (true) {
        choice = 0;
        cout << "Welcome in employee program. Please, enter your choice:" << endl;
        cout << "\t 1 - Add new employee" << endl;
        cout << "\t 2 - Show all of employees by gender" << endl;
        cout << "\t 3 - Show all employees by age range" << endl;
        cout << "\t 4 - Calculate salary for employee" << endl;
        cout << "\t 5 - Show employee details" << endl;
        cout << "\t 6 - Exit" << endl;
        cin >> choice;
        switch (choice) {
            case 1:
                for (int i = 0; i < 10; i++) {
                    if (people[i].available == 0) {
                        id = i;
                        current = &people[i];
                    }
                }
                if (current == 0) {
                    cout << "Sorry, but database is full and you can't add items anymore" << endl;
                }
                else {
                    current->available = 1;
                    current->id = id;
                    cout << "Enter name: ";
                    cin.ignore();
                    getline(cin, current->name);
                    cout << "Age: ";
                    cin >> current->age;
                    cout << "Gender (0 - male, 1 - female): ";
                    cin >> current->gender;
                    cout << "Hourly rate: ";
                    cin >> current->hourlyRate;
                    cout << "Hours worked: ";
                    cin >> current->hours;
                    cout << endl << endl;
                    current = 0;
                }
                break;
            case 2:
                cout << "Enter 0 to show all male employees and 1 - to show all female employees: ";
                cin >> choice;
                cout << endl;
                if (choice) {
                    cout << "Female employees:" << endl;
                    for (int i = 0; i < 10; i++) {
                        if (people[i].available == 1 && people[i].gender > 0) {
                            cout << "ID: " << people[i].id << "\t NAME: " << people[i].name << endl;
                        }
                    }
                }
                else {
                    cout << "Male employees:" << endl;
                    for (int i = 0; i < 10; i++) {
                        if (people[i].available == 1 && people[i].gender == 0) {
                            cout << "ID: " << people[i].id << "\t NAME: " << people[i].name << endl;
                        }
                    }
                }
                cout << endl << endl;
                break;
            case 3:
                cout << endl << endl << "\nEnter minimal age: ";
                cin >> minAge;
                cout << "Enter maximum age: ";
                cin >> maxAge;
                for (int i = 0; i < 10; i++) {
                    if (people[i].available == 1 && minAge <= people[i].age && people[i].age < maxAge) {
                        cout << "ID: " << people[i].id << "\t NAME: " << people[i].name << endl;
                    }
                }
                cout << endl << endl;
                break;
            case 4:
                cout << "Type employee ID: ";
                cin >> choice;
                if (choice >= 0 && choice < 10 && people[choice].available == 1) {
                    cout.precision(4);
                    cout << "Employee's salary: " << people[choice].calculateSalary() << " USD " << endl << endl;
                }
                else {
                    cout << "Something is wrong with employee ID" << endl;
                }
                break;
            case 5:
                cout << "Type employee ID: ";
                cin >> choice;
                if (choice >= 0 && choice < 10 && people[choice].available == 1) {
                    people[choice].showDetails();
                }
                cout << endl;
                break;
            case 6:
                return 0;
                break;
        }
    }
    return 0;
}


https://www.AssignmentExpert.com

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Niks632555
06.05.15, 17:42

Can you make this code without using class.?

Assignment Expert
05.05.15, 18:04

Dear Mohamed, please find fixed answer above. We are glad to be helpful. If you liked our service please press like-button beside answer field. Thank you!

Mohamed
01.05.15, 20:19

That's a big help. Thanks a lot. I really appreciate it. But in this part it gives a compile error: cout

LATEST TUTORIALS
APPROVED BY CLIENTS