Instructions
Requirements and Specifications
● /******************************************************************************
● PROGRAM NAME: aSimpleCalculator.cpp
● DATE: 6 May 2019
● VERSION: 1.0
● AUTHOR: FH BERMUDEZ
● DESCRIPTION: This program demonstrates implementation of a simple
● (+, -, *, and /) calculator using modular design and proper documentation.
● **/
● #include<iostream>
● #include<string>
●
● using namespace std;
●
●
● // Function Prototypes
● void runApplication();
● void displayMenu();
● void getUserChoice();
● void executeOperation();
● void MERCURY();
● void VENUS();
● void pressEnterToContinue();
● void clearConsole();
●
● string planet;
●
●
● // Global Constants
● char const EXIT = '0';
● int const MAX_STRING_LENGTH = 100;
●
● // Global Variables
● char userChoice = EXIT;
●
● int main(int argc, char** argv)
● {
● runApplication();
● } // End of main
●
● /******************************************************************************
● FUNCTION NAME: runApplication
● PARAMETER(S): none
● RETURN TYPE: void
● VERSION: 1.0
● DATE: 6 May 2019
● AUTHOR: FH BERMUDEZ
● DESCRIPTION: This method is the heart of the application, it calls the
● methods required to run the application.
● **/
● void runApplication()
● {
● do {
● clearConsole();
● displayMenu();
● getUserChoice();
● executeOperation();
● } while (userChoice != EXIT);
● } // End of runApplication
●
● /******************************************************************************
● FUNCTION NAME: displayMenu
● PARAMETER(S): none
● RETURN TYPE: void
● VERSION: 1.0
● DATE: 6 May 2019
● AUTHOR: FH BERMUDEZ
● DESCRIPTION: This method display a simple menu and ask the user to enter
● the operation to perform, then calls the executeOperation method.
● **/
● void displayMenu()
● {
● cout << " ____________________________________\n"
● "|This program calculates the weight |\n"
● "|of a person in one of the celestial |\n"
● "|bodies: mercury, venus, earth, mars,|\n"
● "|jupiter, saturn, uranus, neptune, |\n"
● "|pluto and moon. |\n"
● "|____________________________________|\n"
● "| MENU |\n"
● "| 0. Exit Program |\n"
● "| 1. Calculate the Weight |\n"
● "|____________________________________|\n"
● " Enter your choice: ";
●
● } // End of displayMenu
●
● /******************************************************************************
● FUNCTION NAME: getUserChoice
● PARAMETER(S): none
● RETURN TYPE: void
● VERSION: 1.0
● DATE: 6 May 2019
● AUTHOR: FH BERMUDEZ
● DESCRIPTION: This method gets the choice from the user as an "int" and
● stores the value in the global variable userChoice.
● **/
● void getUserChoice()
● {
● cin >> userChoice; // Save the user input in the global variable
● cout << endl;
● } // End of getUserChoice
●
● /******************************************************************************
● FUNCTION NAME: executeOperation
● PARAMETER(S): none
● RETURN TYPE: void
● VERSION: 1.0
● DATE: 6 May 2019
● AUTHOR: FH BERMUDEZ
● DESCRIPTION: This method get the user choice and execute the selected
● operation
● **/
● void executeOperation()
● {
● switch(userChoice)
● {
● case '0': cout << "Exiting Program" << endl;
● break;
● case '1': cout << "Enter the name of a planet: " << endl;
● cin >> planet;
● if (MERCURY) MERCURY();
● if (VENUS) VENUS();
● break;
● default: cout << "Invalid Choice, Try Again" << endl <<endl;
● break;
● } // End of switch
●
● pressEnterToContinue();
● cout << endl << endl;
● } // End of executeOperation
●
● /******************************************************************************
● FUNCTION NAME: mercury
● PARAMETER(S): none
● RETURN TYPE: void
● VERSION: 1.0
● DATE: 6 May 2019
● AUTHOR: FH BERMUDEZ
● DESCRIPTION: This method ask the user to enter two numbers to add.
● **/
● void MERCURY()
● {
●
● int number = 0;
●
●
● cout << "Enter your weight in pounds: ";
● cin >> number;
● cout << "Your weight of " << number << "lbs on mercury is: "
● << number * 0.4115;
● cout << endl << endl;
● } // End of mercury
●
● /******************************************************************************
● FUNCTION NAME: VENUS
● PARAMETER(S): none
● RETURN TYPE: void
● VERSION: 1.0
● DATE: 6 May 2019
● AUTHOR: FH BERMUDEZ
● DESCRIPTION: This method ask the user to enter two numbers to subtract.
● **/
● void VENUS()
● {
●
● int number = 0;
●
●
● cout << "Enter your weight in pounds: ";
● cin >> number;
● cout << "Your weight of " << number << "lbs on venus is: "
● << number * 0.8975;
● cout << endl << endl;
● } // End of venus
●
● /******************************************************************************
● FUNCTION NAME: pressEnterToContinue
● PARAMETER(S): none
● RETURN TYPE: void
● VERSION: 1.0
● DATE: 6 May 2019
● AUTHOR: FH BERMUDEZ
● DESCRIPTION: This method implements the "Press Enter" to continue task,
● using cin and .ignore method.
● **/
● void pressEnterToContinue()
● {
● char key;
● cin.clear(); //clears the error flag on cin
● //so that future I/O operations will work correctly.
● cin.ignore(MAX_STRING_LENGTH, '\n'); // ignore until <Enter> key
● cout << "Press [Enter] to continue . . .";
● key = cin.get();
● } // End of pressEnterToContinue
●
● /******************************************************************************
● FUNCTION NAME: clearConsole
● PARAMETER(S): none
● RETURN TYPE: void
● VERSION: 1.0
● DATE: 6 May 2019
● AUTHOR: FH BERMUDEZ
● DESCRIPTION: This method clears the screen console using the system()
● method call.
● **/
● void clearConsole()
● {
● system("cls"); // use "cls" for windows, "clear" for linux
● } // End of clearConsole
Source Code
/******************************************************************************
PROGRAM NAME: aSimpleCalculator.cpp
DATE: 6 May 2019
VERSION: 1.0
AUTHOR: FH BERMUDEZ
DESCRIPTION: This program demonstrates implementation of a simple
(+, -, *, and /) calculator using modular design and proper documentation.
**/
#include<iostream>
#include<string>
using namespace std;
// Function Prototypes
void runApplication();
void displayMenu();
void getUserChoice();
void executeOperation();
void MERCURY();
void VENUS();
void pressEnterToContinue();
void clearConsole();
string planet;
// Global Constants
char const EXIT = '0';
int const MAX_STRING_LENGTH = 100;
// Global Variables
char userChoice = EXIT;
int main(int argc, char** argv)
{
runApplication();
} // End of main
/******************************************************************************
FUNCTION NAME: runApplication
PARAMETER(S): none
RETURN TYPE: void
VERSION: 1.0
DATE: 6 May 2019
AUTHOR: FH BERMUDEZ
DESCRIPTION: This method is the heart of the application, it calls the
methods required to run the application.
**/
void runApplication()
{
do {
clearConsole();
displayMenu();
getUserChoice();
executeOperation();
} while (userChoice != EXIT);
} // End of runApplication
/******************************************************************************
FUNCTION NAME: displayMenu
PARAMETER(S): none
RETURN TYPE: void
VERSION: 1.0
DATE: 6 May 2019
AUTHOR: FH BERMUDEZ
DESCRIPTION: This method display a simple menu and ask the user to enter
the operation to perform, then calls the executeOperation method.
**/
void displayMenu()
{
cout << " ____________________________________\n"
"|This program calculates the weight |\n"
"|of a person in one of the celestial |\n"
"|bodies: mercury, venus, earth, mars,|\n"
"|jupiter, saturn, uranus, neptune, |\n"
"|pluto and moon. |\n"
"|____________________________________|\n"
"| MENU |\n"
"| 0. Exit Program |\n"
"| 1. Calculate the Weight |\n"
"|____________________________________|\n"
" Enter your choice: ";
} // End of displayMenu
/******************************************************************************
FUNCTION NAME: getUserChoice
PARAMETER(S): none
RETURN TYPE: void
VERSION: 1.0
DATE: 6 May 2019
AUTHOR: FH BERMUDEZ
DESCRIPTION: This method gets the choice from the user as an "int" and
stores the value in the global variable userChoice.
**/
void getUserChoice()
{
cin >> userChoice; // Save the user input in the global variable
cout << endl;
} // End of getUserChoice
/******************************************************************************
FUNCTION NAME: executeOperation
PARAMETER(S): none
RETURN TYPE: void
VERSION: 1.0
DATE: 6 May 2019
AUTHOR: FH BERMUDEZ
DESCRIPTION: This method get the user choice and execute the selected
operation
**/
void executeOperation()
{
switch(userChoice)
{
case '0': cout << "Exiting Program" << endl;
break;
case '1': cout << "Enter the name of a planet: " << endl;
cin >> planet;
if (planet == "MERCURY")
MERCURY();
else if (planet == "VENUS")
VENUS();
else
cout << "Invalid Planet, Please enter VENUS or MERCURY" << endl <<endl;
break;
default: cout << "Invalid Choice, Try Again" << endl <<endl;
break;
} // End of switch
pressEnterToContinue();
cout << endl << endl;
} // End of executeOperation
/******************************************************************************
FUNCTION NAME: mercury
PARAMETER(S): none
RETURN TYPE: void
VERSION: 1.0
DATE: 6 May 2019
AUTHOR: FH BERMUDEZ
DESCRIPTION: This method ask the user to enter two numbers to add.
**/
void MERCURY()
{
int number = 0;
cout << "Enter your weight in pounds: ";
cin >> number;
cout << "Your weight of " << number << "lbs on mercury is: "
<< number * 0.4115;
cout << endl << endl;
} // End of mercury
/******************************************************************************
FUNCTION NAME: VENUS
PARAMETER(S): none
RETURN TYPE: void
VERSION: 1.0
DATE: 6 May 2019
AUTHOR: FH BERMUDEZ
DESCRIPTION: This method ask the user to enter two numbers to subtract.
**/
void VENUS()
{
int number = 0;
cout << "Enter your weight in pounds: ";
cin >> number;
cout << "Your weight of " << number << "lbs on venus is: "
<< number * 0.8975;
cout << endl << endl;
} // End of venus
/******************************************************************************
FUNCTION NAME: pressEnterToContinue
PARAMETER(S): none
RETURN TYPE: void
VERSION: 1.0
DATE: 6 May 2019
AUTHOR: FH BERMUDEZ
DESCRIPTION: This method implements the "Press Enter" to continue task,
using cin and .ignore method.
**/
void pressEnterToContinue()
{
char key;
cin.clear(); //clears the error flag on cin
//so that future I/O operations will work correctly.
cin.ignore(MAX_STRING_LENGTH, '\n'); // ignore until <Enter> key
cout << "Press [Enter] to continue . . .";
key = cin.get();
} // End of pressEnterToContinue
/******************************************************************************
FUNCTION NAME: clearConsole
PARAMETER(S): none
RETURN TYPE: void
VERSION: 1.0
DATE: 6 May 2019
AUTHOR: FH BERMUDEZ
DESCRIPTION: This method clears the screen console using the system()
method call.
**/
void clearConsole()
{
system("clear"); // use "cls" for windows, "clear" for linux
} // End of clearConsole