# Help with making a calculator

**URL:** <https://discourse.processing.org/t/help-with-making-a-calculator/24631>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [October 16, 2020, 1:27am UTC](https://discourse.processing.org/t/help-with-making-a-calculator/24631 "2020-10-16T01:27:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yaboi999](https://avatars.discourse-cdn.com/v4/letter/y/258eb7/32.png) [@yaboi999](https://discourse.processing.org/u/yaboi999)\
**Post date:** [October 16, 2020, 1:27am UTC](https://discourse.processing.org/t/help-with-making-a-calculator/24631/1 "2020-10-16T01:27:09Z")

</div>

Hello,

I need help finishing my calculator. Unfortunatey the teacher has not been much help, and I have struggled with this for two and a half weeks now.

This is my code. I will attach my flowchart as well. Please help.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/695175cb8f9ea0984bdc1a2739cef9065aab48c0.png)

```auto
int numButtons, wd, ht, choice;

Float acc = 0.0;
int op = 0;
float result = 0;
String num = "";
boolean decPoint = false;

//declares arrays for drawing boxes locations
//0---------------0----1----2----3----4----5----6----7----8----9 + _ * / = . C
int[] x = {200, 150, 200, 250, 150, 200, 250, 150, 200, 250, 300, 300, 300, 300, 300, 300, 300};
int[] y = {250, 200, 200, 200, 150, 150, 150, 100, 100, 100, 0, 50, 100, 150, 200, 250, 300};
String keys[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-", "*", "/", "=", ".", "C"};
void setup() {
  size(500, 500);
  numButtons = 16;
  wd = 45;
  ht = 45;

  for (int boxes = 0; boxes < 16; boxes = boxes+1) //draws all the boxes and puts labels on them
  {
    fill(250, 250, 230);
    rect(x[boxes], y[boxes], wd, ht);
    fill(50);
    text(keys[boxes], (x[boxes] + 20), (y[boxes] + 25));
  }
}

void draw() {
}

void mouseReleased() //sense the clicks on the boxes
{  
  for (int i = 0; i < numButtons; i++) { //loop to sense and print number clicks
    if ((x[i] < mouseX) && (mouseX < (x[i] + wd)) && (y[i] < mouseY) && (mouseY < (y[i]+ht))) {
      choice = i; //if mouse is clicked, set i to choice
    }
  }
  if (choice >= 0 && choice <= 9) { //digit?
    num = num + choice; //add to num
  } else if (choice == 15) { //point?
    if (decPoint == false) { //if no dp, add decimal to number and set to true
      num = num + '.';
      decPoint = true;
    } else if (decPoint == true) //if dp, print an error message
      println("Only one decimal allowed!");
  } else if (choice >= 10 && choice <= 13) { //operator?
    if (op == 0) { //if no current op, set op, and num is assigned to acc
      op = choice;
      acc = float(num);
    }
    if (op == 10){
      acc = (acc + float(num));
      op = 0;
      println(acc);
  }
    num = ""; //resets num
    decPoint = false; //resets decpoint
  }
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [October 16, 2020, 1:47am UTC](https://discourse.processing.org/t/help-with-making-a-calculator/24631/2 "2020-10-16T01:47:21Z")

</div>

> [@yaboi999](#):
>
> Unfortunatey the teacher has not been much help, and I have struggled with this for two and a half weeks now.

Please focus your energy on your project and not blaming your instructor.

What do you need help with?

> [@Guidelines—Asking Questions](https://discourse.processing.org/t/guidelines-asking-questions/2147):
>
> Summary (TL;DR) Ask complete questions to get better answers! Be specific. For example: I want to load an image. I tried using createImg() , and I expected the image to be on canvas, but what happened instead was the image showing up below the canvas. Isolate your problem and work in small steps. Share only the code directly related to your problem if it is part of a bigger project, and if something isn’t working, try the smallest code that could do the thing you want. Can we run your code to …

---

<div class="post-metadata">

**Author:** ![yaboi999](https://avatars.discourse-cdn.com/v4/letter/y/258eb7/32.png) [@yaboi999](https://discourse.processing.org/u/yaboi999)\
**Post date:** [October 16, 2020, 2:12am UTC](https://discourse.processing.org/t/help-with-making-a-calculator/24631/3 "2020-10-16T02:12:35Z")

</div>

I don’t know how to make the operators work. I don’t even know what I’m doing wrong. As far as I can see, the code matches the flowchart (as far its written), but I cannot get it to work.

It either only does addition, or only does one thing, and if I try to add a op = 0 statement to reset it back to no operator, it does not work (I don’t even know how to describe what it does instead). I don’t know what I need help with beyond this, because I can’t pinpoint where I’m going wrong.

I was mostly hoping that someone could look over what I have and give some suggestions because I’m lost on where to go next. My school does not offer tutoring, and I can’t find anything online other than forums and this is the only forum I get responses from.

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 17, 2020, 10:05am UTC](https://discourse.processing.org/t/help-with-making-a-calculator/24631/4 "2020-10-17T10:05:10Z")

</div>

> [@yaboi999](#):
>
> because I can’t pinpoint where I’m going wrong.

The best way is to use println("describe the purpose ", variable); wherever you can. This way you easy check the flow.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 17, 2020, 3:50pm UTC](https://discourse.processing.org/t/help-with-making-a-calculator/24631/5 "2020-10-17T15:50:35Z")

</div>

You can concentrate on 3+4 =

Then 13+14=

Then 13.5 + 4 =

(Skip 13+5\*3 for now.)

Now. Normally you collect numbers. First number. Then when + comes store this separately (extra variable). Collect number two.

Only when the = is entered, start acting: take number 1 then operator then number two.

Does it work this far?

(the longer formula is more difficult)
