Answer to Question #171074 in C++ for anas

Question #171074

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


1
Expert's answer
2021-03-11T23:24:06-0500



#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;
}




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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS