# Visual Studio C# Integer Calculator Help



## KeyesZX (Aug 1, 2005)

I am currently enrolled in a programming class and we are leaning the C programming language. The software that we are using the develop is Visual Studio. Right now, we are creating integer calculators. Now, I have never programmed before, so this is all new to me. The teacher told us to try and get subtraction, multiplication, and division working. I can get them to work; however, only one at a time. I have attached the code thus far. That's what he showed us.

My problem is, I am stuck. I can change the equals button and such so that it can divide etc, but the other functions (ie: addition etc) will not work. If anyone could explain to me how to get subtraction to work, then I could figure out the rest. Anything that points me in the right direction would get awesome. It's frustrating because I have the feeling that it's going to turn out to be something really obvious and easy to do  .


----------



## dquigley (Apr 17, 2006)

Think about how a simple calculator works in real life. You are not modeling the problem correctly.

1) You enter a number
2) You enter an operation (+ - / *)
3) You enter another number
4) You hit the equals or another operation and a result is displayed

Now think about what has to happen in a calculator's memory to make this work:

1) Where does the first number get stored and how does the calculator know when to store it?
2) Where/how does the operation (+ - / * ) get stored?
3) Where does the second number get stored or does it need to be stored?
4) Where does the result get displayed

Name your variables to mirror what happens in real life.

as in:

display
last_operation
accumulator

You need to look over how you process the number keys. You currently process them as text, which is OK, but I would be tempted to keep everything in numeric variables to model a real calculator.

1) Press a 3
display = 3
2) Press a 4
display = 34
3) Press a 1
display = 341

there are a couple of ways to make that happen one that comes to mind is:

display = (display * 10) + keystroke_value

Also, most programming classes build new lessons on the code you write so think about what would change if you are going to be asked to modify your calculator to perform in full decimal mode (e.g. what would you need to process the decimal keystroke?)

Best,
Dan


----------

