Answer:
#include<iostream>
#include<limits>
#include<iomanip>
using namespace std;
//function that returns true
//if there is invalid input like giving string as input
// for integer value.
bool isInvalidInput()
{
//return true if cin.fail
if(cin.fail())
{
//clear the buffer
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
return true;
}
return false;
}
//function that calculates the hire score for given 3 parameters.
double calHireScore(double agility,double strength,double speed)
{
return (1.8*agility) + (2.16 * strength) + (3.24 * speed);
}
//calculate hire score for fox.
void calFoxHireScore()
{
double agility,strength;
cout<<"Enter Agility Value: ";
//get agility as input and validate it.
cin >> agility;
if(isInvalidInput() || agility <= 0)
{
cout<<"\nError: Agility value must be > 0"<<endl;
return;
}
cout<<"Enter Strength Value: ";
cin >> strength;
if(isInvalidInput() || strength <= 0)
{
cout<<"\nError: Strength value must be > 0"<<endl;
return;
}
cout<<"\nHire Score for Fox: "<<calHireScore(agility,strength,0)<<endl;
}
//function that asks input from user for
//calculating hire score for Sloth.
void calSlothHireScore()
{
double strength,speed;
cout<<"Enter Strength Value: ";
cin >> strength;
if(isInvalidInput() || strength <= 0)
{
cout<<"\nError: Strength value must be > 0"<<endl;
return;
}
cout<<"Enter Speed Value: ";
cin >> speed;
if(isInvalidInput() || speed <= 0)
{
cout<<"\nError: Speed value must be > 0"<<endl;
return;
}
cout<<"\nHire Score for Sloth: "<<calHireScore(0,strength,speed)<<endl;
}
//function that asks input from user for
//calculating hire score for Bunny.
void calBunnyHireScore()
{
double agility,speed;
cout<<"Enter Agility Value: ";
cin >> agility;
if(isInvalidInput() || agility <= 0)
{
cout<<"\nError: Agility value must be > 0"<<endl;
return;
}
cout<<"Enter Speed Value: ";
cin >> speed;
if(isInvalidInput() || speed <= 0)
{
cout<<"\nError: Speed value must be > 0"<<endl;
return;
}
cout<<"\nHire Score for Bunny: "<<calHireScore(agility,0,speed)<<endl;
}
//function to display menu for user.
void menu()
{
cout<<"\n1. Fox"<<endl;
cout<<"2. Bunny"<<endl;
cout<<"3. Sloth"<<endl;
cout<<"0. Quit"<<endl;
cout<<"Enter your choice: ";
}
//main method
int main()
{
int choice;
do
{
//asks choice
menu();
cin >> choice;
//if invalid input print erro message
if(isInvalidInput())
{
cout<<"\nError: Invalid choice enetered. Try Again\n";
}
else
{
cout<<"\n";
//if any one of the choices do accordingly
//for invalid choice print error mesage.
switch(choice)
{
case 1:
calFoxHireScore();
break;
case 2:
calBunnyHireScore();
break;
case 3:
calSlothHireScore();
break;
case 0:
cout<<"\nQutting...\n";
break;
default:
cout<<"Invalid choice entered."<<endl;
break;
}
}
}while(choice != 0);
return 0;
}
Explanation: