Kat H.

asked • 06/30/21

Write a C++ calculator program that keeps track of a subtotal like real calculators do.

Start by asking the user for an initial number. Inside a loop, asks the user for a mathematical operation and a second number. Do the operation with the first number on the second num- ber. Keep track of the result as a 'subtotal' to be used as the first number when going back to the top of the loop. When the user quits, print out the final result and quit the program. See output for an example for the behavior of the program.


The operations should be at least:

• add

• subtract

• multiply

• divide

...but you could add other operations as well.


Requirements


You need to have at least 3 functions other than main in your code. One way to achieve this is to make each operation a function but there may be other ways to break up the code into functions, such as by task: printMenu, getOperation, doCalculation, etc.


You need to keep a running total which will be the previous result of the previous operation (this will be the first number for next operation). Print this total after each operation.


Check for the second number being 0 before doing division.

All functions must be callable from main or another function and do something useful.


-----------------------


EXAMPLE OUTPUT #1

-----------------------


*** Calculator Program ***

Enter first Number:

10

*** operations ***

a -add

s - subtract

m - multiply

d - divide

q - quit program

choice: m

Enter second Number 12

The subtotal is: 120 *** operations ***

a -add

s - subtract

m - multiply

d - divide

q - quit program

choice: s

Enter second Number 9

The subtotal is: 111 *** operations ***

a -add

s - subtract

m - multiply

d - divide

q - quit program

choice: d

Enter second Number 0

can't divide by zero.

The subtotal is: 111 *** operations ***

a -add

s - subtract

m - multiply

d - divide

q - quit program

choice: d

Enter second Number 10

The subtotal is: 11.1 *** operations ***

a -add

s - subtract

m - multiply

d - divide

q - quit program

choice: a

Enter second Number

23

The subtotal is: 34.1 *** operations ***

a -add

s - subtract

m - multiply

d - divide

q - quit program

choice: q

The Final Total is: 34.1

*** Thank you for using our app!

1 Expert Answer

By:

Patrick B. answered • 07/01/21

Tutor
4.7 (31)

Math and computer tutor/teacher

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.