I need to write functions by creating a simple menusystem that will perform forc
ID: 3691175 • Letter: I
Question
I need to write functions by creating a simple menusystem that will perform force calculations.
I need to write 5 functions.
int main()
// this will call handleMenuControl void handleMenuControl()// this will control the menu program
// including calling the function that outputs the
// menu, handling user input, and calling the
// correct version of force void displayMenu()
// this function is only responsible for displaying// the menu double force(double mass)
// this function will return the result of a force
// calculation that uses the mass argument and
// assumes acceleration is 9.8 m/s^2
void force(double mass, double acceleration, double& result)
// The above function receives arguments mass and acceleration by value
// they should correspond to user input. The result is returned by storing
// the answer in pass by reference argument result
“So this is a sample of what it should output:”
Calculation Menu:
please select from one of the following or enter 3 to quit: 1.
Peform force calculation with acceleration as 9.8m/s^2 2.
Perform force calculation, specifying accelerationSelection: 1 Enter mass (assumed in kg): 5
Result: 49
Calculation Menu:
please select from one of the following or enter 3 to quit: 1.
Peform force calculation with acceleration as 9.8m/s^2 2.
Perform force calculation, specifying acceleration Selection:
2 Enter mass (assumed in kg): 7.9
Enter acceleration (assumed in m/s^2): 9.8
Result: 77.42
Calculation Menu:
please select from one of the following or enter 3 to quit: 1.
Peform force calculation with acceleration as 9.8m/s^2 2.
Perform force calculation, specifying acceleration Selection:
2 Enter mass (assumed in kg): 7.9
Enter acceleration (assumed in m/s^2): 4
Result: 31.6
Calculation Menu:
please select from one of the following or enter 3 to quit: 1.
Peform force calculation with acceleration as 9.8m/s^2 2.
Perform force calculation, specifying acceleration Selection:4
Invalid input, please enter a number from 1 to 3
Calculation Menu:
please select from one of the following or enter 3 to quit: 1.
Perform force calculation with acceleration as 9.8m/s^2 2.
Perform force calculation, specifying acceleration
My notes:
(Function handleMenuControl exists and correctly handles control of the menu system, including all user input)
(function double force(double mass) is included and correctly returns a double to the caller)
(function void force(double mass, double acceleration, double& result is included and correctly utilizes the pass by reference parameter)
Explanation / Answer
#include <iostream>
#include <stdlib.h>
using namespace std;
void handleMenuControl();
void displayMenu();
double force(double mass);
void force(double mass, double acceleration, double& result);
int main ()
{
handleMenuControl();
}
void handleMenuControl()
{
displayMenu();
double mass = 0;
double acceleration = 0;
double result = 0;
int userin = 0;
cout << "Selection:";
cin >> userin;
if (userin == 1)
{
cout << "Please enter mass (assumed in kg):";
cin >> mass;
force( mass);
}
else if (userin == 2)
{
cout << "Enter mass (assumed in kg):";
cin >> mass;
cout << "Enter acceleration (assumed in m/s^2):";
cin >> acceleration;
force(mass,acceleration, result);}
else if (userin == 3)
{exit(0);}
else
{
cout << "Error" <<endl;
handleMenuControl();}
}
void displayMenu()
{
cout << "**********" <<endl;
cout << " CALCULATION MENU" <<endl;
cout << "********" <<endl;
cout << " ";
cout << "Please select one of the following, or press 3 to quit" <<endl;
cout << "1. Perform force calculation with acceleration as 9.8m/s^2"<< endl;
cout << "2. Perform calculation, specifying acceleration" <<endl;
}
double force(double mass)
{
cout << mass * 9.8;
cout <<"N"<<endl;
handleMenuControl();
}
void force(double mass, double acceleration, double& result)
{
result = mass * acceleration;
cout << result<<"N"<<endl;
handleMenuControl();
}