Help with making a calculator

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.

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
  }
}

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

What do you need help with?

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.

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

1 Like

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)