Basic calculator code error: "it looks like you are mixing active and static modes"

I have a error it looks like you are mixing active and static modes can you help me

My code:

String input = "";

float input1 = 0;
float input2 = 0;
float result = 0;

int operator = 0;

boolean drawResult = false;

void setup() {

  background(255);
  size(720, 1280);

  fill(95, 158, 160);

  rect(0, height/5, width, height*4/5);

  textSize(height/10);
  frameRate(90);
  fill(255);
  text("1", 30, height/5*1.7);
  text("2", 30+175, height/5*1.7);
  text("3", 30+175+175, height/5*1.7);
  text("+", 30+175+175+175, height/5*1.7);

  text("4", 30, height/5*2.7);
  text("5", 30+175, height/5*2.7);
  text("6", 30+175+175, height/5*2.7);
  text("-", 30+175+175+175, height/5*2.7);

  text("7", 30, height/5*3.7);
  text("8", 30+175, height/5*3.7);
  text("9", 30+175+175, height/5*3.7);
  text("x", 30+175+175+175, height/5*3.7);

  text("C", 30, height/5*4.7);
  text("0", 30+175, height/5*4.7);
  text("=", 30+175+175, height/5*4.7);
  text("/", 30+175+175+175, height/5*4.7);
}
void draw() { //ekran
  fill(255);
  rect(0, 0, width, height/5);
  fill(220, 20, 60);
  if (drawResult) {
    text(nfc(result,3), 30, 150);
  } else {
    text(input, 30, 150);
  }
}

void mousePressed() {

  if (mouseX >= 0 && mouseX <= 175 && mouseY >= height/4 && mouseY<= height/4+255 ) {
    input = input +1;
    drawResult = false;
  } else if (mouseX >= 175 && mouseX <= 0+175+175 && mouseY >= height/4 && mouseY<= height/4+255) {
    input = input +2;
    drawResult = false;
  } else if (mouseX >= 0+175+175 && mouseX <= 0+175+175+175 && mouseY >= height/4 && mouseY<= height/4+255) {
    input = input +3;
    drawResult = false;
  } else if (mouseX >= 0+175+175+175 && mouseX <= 0+175+175+175+175 && mouseY >= height/4 && mouseY<= height/4+255) {
    println("+");
    drawResult = false;
    input1 = parseInt(input);
    input = "+";
    operator = 1;
    //noStroke();
    //fill(255);
    //rect(0, 0, width, height/4);
    // rad 2
  } else if (mouseX >= 0 && mouseX <= 175 && mouseY >= height/4+255 && mouseY<= height/4+255+255) {
    input = input +4;
    drawResult = false;
  } else if (mouseX >=175 && mouseX <= 175+175 && mouseY >= height/4+255 && mouseY<= height/4+255+255 ) {
    input = input +5;
    drawResult = false;
  } else if (mouseX >= 175+175 && mouseX <= 175+175+175&& mouseY >= height/4+255 && mouseY<= height/4+255+255 ) {
    input = input +6;
    drawResult = false;
  } else if (mouseX >= 175+175+175 && mouseX <= 175+175+175+175&& mouseY >= height/4+255 && mouseY<= height/4+255+255 ) {
    println("-");
    drawResult = false;
    input1 = parseInt(input);
    input = "-";
    //fill(255);
    //rect(0, 0, 667, 212);
    //input = input + "-";
    operator = 2;

    //rad 3
  } else if (mouseX >= 0 && mouseX <= 175&& mouseY >= height/4+255+255 && mouseY<= height/4+255+255+255 ) {
    input = input +7;
    drawResult = false;
  } else if (mouseX >= 175 && mouseX <= 175+175 && mouseY >= height/4+255+255 && mouseY<= height/4+255+255+255 ) {
    input = input +8;
    drawResult = false;
  } else if (mouseX >= 175+175 && mouseX <= 175+175+175 && mouseY >= height/4+255+255&& mouseY<= height/4+255+255+255 ) {
    input = input +9;
    drawResult = false;
  } else if (mouseX >= 175+175+175 && mouseX <= 175+175+175+175 && mouseY >= height/4+255+255 && mouseY<= height/4+255+255+255 ) {
    println("x");
    drawResult = false;
    input1 = parseInt(input);
    input = "x";
    //fill(255);
    //rect(0, 0, 667, 212);
    operator = 3;

    //rad 4
  } else if (mouseX >= 0 && mouseX <= 0+175 && mouseY >= height/4+255+255+255 && mouseY<= height/4+255+255+255+255+255 ) {
    println("C");
    drawResult = false;
    input = "";
    input1 = 0;
    input2 = 0;
    operator = 0;
    //fill(255);
    //rect(0, 0, 665, 200);
  } else if (mouseX >= 175 && mouseX <= 175+175 && mouseY >= height/4+255+255+255&& mouseY<= height/4+255+255+255+255+255) {
    input = input +0;
  } else if (mouseX >= 175+175 && mouseX <= 175+175+175 && mouseY >= height/4+255+255+255 && mouseY<= height/4+255+255+255+255+255 ) {
    println("=");
    input2 = parseInt(input.substring(1)); //.substring(1) removes 1 symbol from the text
    input = input + "=";
}
else if (mouseX >= 175+175+175 && mouseX <= 175+175+175+175 && mouseY >= height/4+255+255+255 && mouseY<= height/4+255+255+255+255+255 ) {
   
    println("/");
    drawResult =false;
    
    input1= parseInt(input.substring(1));
    input = "/";
    
    input = input + "";
    operator = 4;
  }
    //fill(255);
    //rect(0, 0, 900, 300);
    //fill(220,20,60);
    if (operator == 1) {
      result = input1 + input2;
    } else if (operator == 2) {
      result = input1 - input2;
    } else if (operator == 3) {
      result = input1 * input2;
    } else if (operator == 4) {
      result = input1 / input2;
    }
    drawResult = true;
    input = "";
    input1 = 0;
    input2 = 0;
    operator = 0;
  } 
  println(input);
}
1 Like

The reason your code isn’t running is because some code isn’t contained within a function, such as draw();

If you delete the last two lines:

println(input);
}

or move the println() statement into draw then the code works fine

2 Likes

Thank you can you make for me because my divided symbol is not work i work until six hour for it i am beginner it my fors project

Also a lot of your visuals are still contained in the setup function, which will only draw once. I would move pretty much everything in setup() to draw() (except the size(), textSize() and frameRate() function)

1 Like

If you can write it i will very happy i have to make until morning

I know but divided button is not work i tried for example 9/3 it show infinity answer of this problem

It looks like there’s no code that is setting input2 equal to any number, so the denominator will always be zero (hence infinity)

1 Like

How i can add divide ?

So how i can make input 2

What are you running this code on? It is too big to fit my screen and I cannot test

1 Like

I make for my cellphone if you make with pc it can be too big if you write i can test and i can say yo

I hope you can help me i have to finish until this morning.

Oh ok. Well I was wrong about the input2 thing because I noticed you actually do have code for this. I would print input2 after parsing the ordinary input text and test different numbers. If the result is always 0 then it may be a problem with parsing

println("=");
    input2 = parseInt(input.substring(1)); //.substring(1) removes 1 symbol from the text
    println(input2);
    input = input + "=";

If this is the case then try adding a radix to your parseInt()

input2 = parseInt(input.substring(1), 10); //.substring(1) removes 1 symbol from the text

1 Like

Oh. It’s actually a problem I’ve introduced into their code by helping them in another thread by adding .substring(1) to the code above, so that input2 can be +3 or -3 or x3 or /3 and each would be parsed as 3, yet I didn’t notice that they don’t actually add the / symbol, so it gets sent as 3, goes through .substring(1) that removes the first and only character, turns into nothingness, which is then parsed as 0.

Solution?

Make your code for / (divide) set input as /, exactly the same way other + - x functions do it.

Try figuring that out yourself! You gotta start somewhere doing that and not have other people write it for you, isn’t that right? :smiley:

1 Like