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
Comments
Can you make this code without using class.?
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!
That's a big help. Thanks a lot. I really appreciate it. But in this part it gives a compile error: cout