×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Create A Program to Create a Simple Calculator in C++ Assignment Solution

July 02, 2024
Len E. Villasenor
Len E.
🇺🇸 United States
C++
Len E. Villasenor, with a master’s in computer science from Northern Arizona University, boasts 5 years of expertise in C++ assignment assistance, providing exceptional guidance in this specialized area.
Key Topics
  • Instructions
  • Requirements and Specifications
Tip of the day
Familiarize yourself with OCaml's pattern matching; it simplifies handling recursive data structures like lists and trees, making your code concise and easier to debug.
News
In 2024, Girls Who Code introduced a Data Science + AI track in their free summer programs for high school students, fostering skills in cybersecurity and creative coding​

Instructions

Objective
Write a C++ assignment program to create a simple calculator.

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
● #include
● 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 <
● 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 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

#include

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 <

              break;

    default: cout << "Invalid Choice, Try Again" << 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 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

Related Samples

Explore our C++ Assignment Samples for precise solutions to programming challenges. Covering topics like object-oriented programming, templates, and standard library usage, these examples provide clear explanations and step-by-step implementations. Perfect for students aiming to enhance their C++ proficiency and excel in their coursework with practical, educational resources.p>