My code isn't working how it's supposed to

the code is supposed to print the sum before looping back, but it doesn't do that.
Can you tell me why?


char answer;


int a, b, sum, product, quotient, difference;

void setup() {
  Serial.begin(9600);
}


void loop() {
  Serial.print("Enter a first number: ");

  while (!Serial.available()) {}

  // “!Serial.available” is equivalent to “Serial.available == 0”
  a = Serial.parseInt();

  Serial.println(a);

  Serial.print("Enter a second number: ");
  while (!Serial.available()) {}
  b = Serial.parseInt();
  Serial.println(b);
  Serial.println("Press Y to multiply your numbers");
  while (!Serial.available()) {}
  answer = ShouldIdis();
  if (answer == 'Y') {
    product = a * b;
    Serial.println(" The product is ");
    Serial.println(product);
  } else if (answer == 'N') {
    Serial.println("Press Y to add your numbers.");
    while (!Serial.available()) {}
    answer = ShouldIadd();
    if (answer == 'yes') {
      sum = a + b;
      Serial.println(" The sum is ");
      Serial.println(sum);
    }
  }
}
char ShouldIdis() {
  char yesorno;
  while (!Serial.available()) {}
  yesorno = Serial.read();

  return yesorno;
}
char ShouldIadd() {
  char yorn;
  while (!Serial.available()) {}
  yorn = Serial.read();

  return yorn;
}

This is a great example where some extra, temporary’serial prints’ for debugging intermediate values is worthwhile.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.