company insures its workers in the following cases: If the workers is female and married. If the workers is married, male and above 65 years of age. If the driver is unmarried, female and above 45 year Write C++ a program to determine whether the driver is insured or not, using switch statement only
#include <iostream>
using namespace std;
struct Employee {
string const sex{ "Male", "Female" };
unsigned const age;
bool const married;
};
const bool insured(const Employee* e) {
switch (e->sex[0]) {
case 'M':
switch (e->married) {
case false: return false;
case true:
switch (e->age > 65) {
case false: return false;
case true: return true; // married, male and above 65 years
}
}
case 'F':
switch (e->married) {
case true: return true; // female and married
case false:
switch (e->age > 45) {
case false: return false;
case true: return true; // unmarried, female and above 45 year
}
}
default:
cout << "Wrong symbol input";
exit(-1);
}
}
int main() {
Employee* John = new Employee {"Male", 24, true};
if (insured(John))
cout << John->sex << ", " << John->married << ", " << John->age << " \t: insured" << endl;
else
cout << John->sex << ", " << John->married << ", " << John->age << " \t: not insured" << endl;
Employee* Lynda = new Employee{ "Female", 55, false };
if (insured(Lynda))
cout << Lynda->sex << ", " << Lynda->married << ", " << Lynda->age << " \t: insured" << endl;
else
cout << Lynda->sex << ", " << Lynda->married << ", " << Lynda->age << " \t: not insured" << endl;
delete John;
delete Lynda;
return 0;
}
Comments
Leave a comment