203k views
5 votes
The Zootopia Police Department is recruiting new officers and has come up with an innovative equation to hire. They define hireScore as a weighted sum of agility and strength.hireScore = 1.8 * agility + 2.16 * strength + 3.24 * speedThe candidates for this hiring season are foxes, bunnies and sloths. Chief Bogo has requested you to write a program that takes in these attributes and produces an hireScore for the candidate.The program should provide a menu to choose the anthropomorphic animal. The menu should have the___________.

A. FoxB. BunnyC. SlothD. Quit

User Myxaxa
by
5.3k points

1 Answer

2 votes

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(),'\\');

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<<"\\Error: Agility value must be > 0"<<endl;

return;

}

cout<<"Enter Strength Value: ";

cin >> strength;

if(isInvalidInput() || strength <= 0)

{

cout<<"\\Error: Strength value must be > 0"<<endl;

return;

}

cout<<"\\Hire 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<<"\\Error: Strength value must be > 0"<<endl;

return;

}

cout<<"Enter Speed Value: ";

cin >> speed;

if(isInvalidInput() || speed <= 0)

{

cout<<"\\Error: Speed value must be > 0"<<endl;

return;

}

cout<<"\\Hire 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<<"\\Error: Agility value must be > 0"<<endl;

return;

}

cout<<"Enter Speed Value: ";

cin >> speed;

if(isInvalidInput() || speed <= 0)

{

cout<<"\\Error: Speed value must be > 0"<<endl;

return;

}

cout<<"\\Hire Score for Bunny: "<<calHireScore(agility,0,speed)<<endl;

}

//function to display menu for user.

void menu()

{

cout<<"\\1. 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<<"\\Error: Invalid choice enetered. Try Again\\";

}

else

{

cout<<"\\";

//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<<"\\Qutting...\\";

break;

default:

cout<<"Invalid choice entered."<<endl;

break;

}

}

}while(choice != 0);

return 0;

}

Step-by-step explanation:

User Aleksei Kornushkin
by
5.3k points