[Solved] subtraction on my calculator!

I’ve been struggling with making a very basic Thunkable X calculator for an app of mine for the past 3 weeks, and am still stuck.

I finally got the addition part to work, but the others aren’t working.
SUBTRACTION ERROR:
It returns
-(sum of all the numbers inputted)


Red list-viewer shows all the numbers inputted and the green one shows all the operations.
The equation I gave for subtraction is 6-1.

MULTIPLICATION ERROR:
Returns 0


The equation I gave for multiplication is 4*5.
DIVISION ERROR:
Returns 0

The equation I inputted for division is 9/3.

ADDITION SUCCESS:


The equation for addition is 1+1.
Here’s my code:


It’s the same thing if i is not equal to 0, with one change:

It’s really hard to know without seeing more of your blocks, such as what your list input looks like and what the variable “i” stands for. Can you explain what “i” is for here?

How have you verified that the two numbers you think about being added/subtracted/multiplied/divided are actually the two on the screen? Because I would check that before worrying about how to add/etc. them.

My initial guess is that your list contains text values, not numeric values. Especially if you are having the user type in an input as opposed to tapping a button.

My second thought is that maybe the order of the block assignment isn’t happening the way you’d expect it to. So you might try an intermediate step – at least for testing – where you assign a variable to the first number in the list, assign a variable to the second number in the list, and then finally add/etc. the two numbers.

1 Like

Firstly, I’m using buttons here, as shown in my screenshots.

I’m not necessarily using 2 numbers… I have trouble with this app only because it allows users to operate on as many numbers as they want to.

This is what my i is:

The code is quite messy, so I can PM you the link.

Okay. Things definitely get more complicated when you’re trying to do arithmetic on more than two numbers at a time.

Funny thing is, addition works with more than 2 numbers.

EDIT: I have deleted the project link from my post as the problem is solved by @muneer

1 Like

Your result is consistent with starting from zero and then doing the operation. So adding works, but 6 - 1 is calculated as 0 -6 - 1, and multiplying is 0 * whatever, and dividing is 0 / 9 / 3.

I’d be looking for the glitch on your logic causing that - is the list starting out with a 0 in it? Or is your handling of the first number wrong?

Can you explain the logic of the = button. I got confused. Why there is a set of instructions when i = 0 and another set of instructions when i not equal ZERO?

1 Like

Thank you @domhnallohanlon for the correction. :blush:

1 Like

That is because the ‘for loop’ is starting from 0. The list wouldn’t be able to get the 0th item of the list, so I’m starting from i+1(which is 1) if the i loop is in its first iteration.

1 Like

OK.
So why you started the loop from ZERO. Is there a reason for that?
Why not start with ONE and test only the operators conditions?

1 Like

I made this change and the numbers came correct.
starting the loop from 1
before the loop, store the first number in total so far variable. /your code is not taking care of the first number.
image

2 Likes

Thank you so, so much! This worked. The problem was that I’d initialized my variable total so far to 0, as @catsarisky noticed. Thank you @catsarisky

I’d been working on this calculator for weeks and now it’s finally done!

1 Like

After I added @muneer’s code, it worked a treat!

1 Like

Glad to see it working for you.

1 Like