4-Function Calculator

Conditionals

OK, looking at the single "2+3=5" operation we started with, the program starts out with zero in a result variable, and then displays it. The store-bought calculator, when you turn it on, you can immediately press an operation button (and then a number, which is what it does with the initial zero), or else you can type in a new number (2 in our case), and that replaces the previous result, and becomes the result that the "+3" gets added to. Does your current program work that way? You should be able to "think like a computer" and imagine that you are the one doing the instructions of your computer. What will you do in this program if the user presses "2" first?

Me, I think things are simpler (for the program; later we'll try harder to make things simpler for the user) if the user always enters an operation code, and then a value, and that operation applies that value to the previous result. And then it goes back to the top to apply a new operation + value to the latest result. But that means that coming out of turn-on, the program needs to see an operation, not a number. To add 2+3 the user needs to enter (C)lear first, then two, then (A)dd, then three, and finally (E)qual.

This is your program, so if you want to do it differently, go for it! If you get stuck, come back here and do the unix way (users must do it our way, not the other way around) with me. I'm not overly fond of unix, so I hope you succeed (I know it can be done, but it's a lot more work). Later on (in Java), we will do it the user-friendly way. Otherwise, and in the meantime,

The Unix Way

First the prompt for input. What we want to say probably won't fit in one line on the miniscule text panel in Tom's Kitchen computer, so we might need to split this into two or more lines. Print the user operation choices,
(C)lear, (A)dd, (S)ubtract, (M)ultiply, (D)ivide, (E)qual
You already know how to accept the user input. I would put a length code "1" on the same line so the Kitchen computer knows that one letter is all we want:
Input operation 1
After the user input has been typed in, your program needs to decide what to do next. If they typed "A", you need a second number to add to the running result. If they are just starting and typed "C", you still need a number to start with. That would be another input, and another variable. If you (the programmer) tell the computer that you want a number as input, it will tell the user to enter a number, and nothing else they type will have any effect. You get that with a "-1" on the input line:
Input value -1
You are almost done! The computer doesn't know what the user operation codes mean, so you need to tell it to do that. If the operation is "C" then let the result = value; if the operation is "A" then let the result = result+value. Can you turn that into English? Oh wait, it already is. Be sure to put each command on a separate line. Do you know what these are in the Five Concepts in the table below? Copying a value into a variable is the fourth concept. What about the lines that start with "If"? You are instructing the computer (or a person, if they are "running" the program in their head) to look at whatever operation code the user typed in, then based on whatever it is, conditionally copy or add or whatever. Do you think you can add the other three arithmetic operations? Most computer programming languages use the asterisk (*) to tell the computer to multiply and the slant (/) to tell it to divide. Your user doesn't need to know that, they will be typing M or D, but in your program you use '-' for subtract, '*' for multiply, and so on. Can you do that?

You may have some typing errors or other possible mistakes -- none of us get out programs running correctly on the first cut, we spend a lot of time finding and correcting mistakes. But you can try it. If you can't find your mistakes, don't worry about it, press the "Go Back" button and then well work you through the difficulties.

What's wrong with this program? Even if you got everything you wrote to work perfectly, the calculator didn't calculate anything, it just accepted one operation and one number and then stopped. Think about why that is, and which of the Five Concepts in the table below might solve the problem. Then turn the page.
Five Basic Concepts
Sequence
Iteration
Conditional
Variables
Input/Output

 

<<Previous | ToC | Next >>

[2021 June 3]