Program Program Name Simple Math Calcu
Program Program Name Simple Math Calcu
Simple Math Calculator Program Author: Kyle NoCompile Date Created: 9/28/14 Program Description:
This program performs simple arithmetic calculations. The user enters numbers and the math operation to
perform on those numbers. The program will then display the result of the operation. The program allows
the user to continue entering a math operation and an integer to calculate off of the previous calculation
result. Modified Date: Modified Description:
***********************************************************/
The provided C++ program is a simple calculator designed to perform basic arithmetic
operations—specifically addition and subtraction. Its primary goal is to allow users to input numbers and
choose the desired mathematical operation, then display the calculated result. An additional feature is the
ability for the user to continue calculations using the previous result, creating a dynamic and interactive
calculator session. Despite its straightforward intent, the code contains several syntactic and logical errors
that require correction to function correctly and efficiently.
The program initiates with a welcome message, greeting the user and introducing the calculator's
capabilities. It then prompts the user to input the first integer value, which establishes the initial running
total. The program employs a loop structure that allows for repeated calculations, aligning with user
preference to continue or exit.
Within this loop, the program prompts the user to select a mathematical operation, either addition ('+') or
subtraction ('-'). To ensure valid input, the program should implement input validation, confirming that
only these two options are accepted. Upon valid selection, the user is requested to input a second integer,
which will be combined with the running total based on the chosen operation.
The program defines separate functions for addition and subtraction, promoting modularity and ease of
maintenance. The core calculation is executed within a function that takes the current total, the new input,
and the operation character. After computation, the result is displayed to the user. This process repeats if
the user opts to continue, with the running total updating after each operation.
Critical to the correct functionality of the program are the proper implementation of input handling,
validation, and function logic. The original code includes structural errors, such as incorrect syntax, logic
flaws, and inconsistent naming conventions, which compromise the program’s execution. For instance, the
function prototypes lack proper syntax, and the main function contains variable declarations and function
calls with errors.
> input;'. The validateMathChoice function incorrectly combines conditions 'if (choice == '+' && choice
== '-')', which can never be true simultaneously, whereas it should verify whether the choice is either '+' or
'-'. Additionally, return types of functions like showResult and keepCalculating are mismatched with their
usage or lack proper return statements. Function names also suffer inconsistencies, such as 'GetInteger'
versus 'getInteger', and case sensitivity errors in calls.
Furthermore, the doMath function's switch statement is improperly structured; it evaluates 'switch
(mathFunc == true)', which is illogical, as 'mathFunc' is a character, not a boolean. Proper implementation
requires switching on 'mathFunc' itself, and cases should handle '+' and '-'. The functions doAddition and
doSubtraction should avoid semi-colons after definitions and should be called correctly with matching case
statements. Proper return values and type matching are essential for seamless operation.
The main loop, which uses 'while (keepCalculating);' lacks parentheses around function invocation and
does not invoke the function as 'while (keepCalculating())'. This syntax error prevents the loop from
executing correctly. Additionally, the program’s prompts and outputs must be refined for clarity, including
correcting string outputs, replacing 'cout<
In conclusion, transforming this basic calculator into a functional program requires addressing these
coding issues: syntactical corrections, logical improvements, consistent naming conventions, and proper
function structuring. Implementing comprehensive input validation ensures robustness, while modular
function design promotes clarity. With these revisions, the calculator can serve as an effective tool for
elementary arithmetic operations, illustrating core programming principles and fostering foundational
understanding for novice programmers.
References
Programming in C++, Fall 2020. (2020). University of North Dakota. Retrieved from [Link]
Deitel, P. J., & Deitel, H. M. (2011). C++ How to Program (8th ed.). Pearson Education.
Appleton, L. (2012). C++ Programming: From Problem Analysis to Program Design. Cengage Learning.
Mehra, A. (2018). Effective C++: 55 Specific Ways to Improve Your Programs and Designs.
Addison-Wesley.
Harbison, S. P., & Steele, G. L. (2002). C++: The Complete Reference. McGraw-Hill.