my char functions aren't working for some reason. I'm not sure 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 {
Serial.println("Press Y to add your numbers.");
answer = ShouldIadd();
if (answer == 'Y') {
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;
}
}
}
The post #2 has pointed the serious flaw of your sketch of not having balanced braces ({}). I have presented below the moderated sketch. Compare it with the original sketch and note the differences.
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()) {}
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.print("The product is: ");
Serial.println(product);
}
else
{
Serial.println("Press Y to add your numbers.");
answer = ShouldIadd();
if (answer == 'Y')
{
sum = a + b;
Serial.print("The sum is: ");
Serial.println(sum);
}
}
}
char ShouldIdis()
{ // The error is that a function-definition is not allowed here before the '{' token
char yesorno;
// while (!Serial.available()) {}
yesorno = Serial.read();
return yesorno;
}
char ShouldIadd()
{ // Same here
char yorn;
while (!Serial.available()) {}
yorn = Serial.read();
return yorn;
}