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