My project is to create a decimal adder via voice recognition. Its like a calculator that only does addition. I am using hc-06 bluetooth module to convert my voice to a string. Let's say for example, I say "21+22" then the lcd would print "21 + 22 = 43". I have no problem in displaying my voice message on the lcd. My problem is that the string can't evaluate the equation. I used toInt() but it only recognizes the first integer. Is there a code to recognize the string and convert numerical values to int separated by + then evaluate it by adding the 2 intergers?

  • Please show us the sketch you are using. – VE7JRO May 20 '18 at 16:59

There is no one single function for performing a calculation from a string like you are after in C.

Instead you will have to parse and split your string up into chunks and convert the two numbers into integers (maybe confirming that there is a + between them) and perform the calculation on those two individual integers.

| improve this answer | |

The standard IO library (sscanf) can help you with that:

String s = "21+22";
int x;
int y;
int items = sscanf(s.c_str(), "%d+%d", &x, &y);
if (items != 2) /* error handling */
int z = x + y;


| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.