# Problem with finding the first character of a string

I’m doing a project for uni which takes an input in the form of cnnnnnn, where ‘c’ is an operator (+, -, *, /) and ‘n’ are integers. The program takes the first 3 integers and performs the appropriate operation with the last 3 integers. e.g. +123456 → 579, -500450 → 50, *200020 → 4000.

For some reason the function where I check to see what operator is at the start of the input string isn’t recognising that they are there. Here is the fuction:

String whatOperator(String inputInt) {
char Operator = inputInt.charAt(0);
if (Operator == ‘+’ ) {
plus(inputInt);
} else if (Operator == ‘-’ ) {
minus(inputInt);
} else if (Operator == ‘*’ ) {
multiply(inputInt);
} else if (Operator == ‘/’ ) {
divide(inputInt);
} else {
errorMessage();
}
}

As you can see, the function i supposed to recognise which operator is at the begging of the input string and the proceed to calculate the equation in a different function.

Any help on this would be greatly appreciated. The whole project (so far) is linked aswell.

Resit_Project.ino (2.68 KB)

+123456 --> 579, -500450 --> 50, *200020 --> 4000.

The last value is a floating-point number. Are those also supported?

Is there a reason you posted a snippet instead of the complete code?

The arrows wont be in the final code. The output will just be posted back into the serial monitor, and then will listen for more input.

Floating point numbers will be converted to integers.

Here is the full code:

``````String inputInt;
char Operator;
int intOne;
int intTwo;
//------------------------------------------------------------------------------

String printWelcomeMessage() {
Serial.println("Please enter the integer values you wish to calculate:");
}
//-------------------------------------------------------------------------------

while (!Serial.available()) {};
}

//-------------------------------------------------------------------------------

String errorMessage() {
Serial.println("ERROR");
Serial.println("");
}

//-------------------------------------------------------------------------------

int getIntOne(String inputInt) {
String stringIntOne = inputInt.substring(1, 4);
intOne = stringIntOne.toInt();
}

//-------------------------------------------------------------------------------

int getIntTwo(String inputInt) {
String stringIntTwo = inputInt.substring(4, 7);
intTwo = stringIntTwo.toInt();
}

//-------------------------------------------------------------------------------

String plus(String inputInt) {
getIntOne(inputInt);
getIntTwo(inputInt);
int answer = intOne + intTwo;
}
//-------------------------------------------------------------------------------

String minus(String inputInt) {
getIntOne(inputInt);
getIntTwo(inputInt);
int answer = intOne - intTwo;
}
//-------------------------------------------------------------------------------

String multiply(String inputInt) {
getIntOne(inputInt);
getIntTwo(inputInt);
int answer = intOne * intTwo;
}
//-------------------------------------------------------------------------------

String divide(String inputInt) {
getIntOne(inputInt);
getIntTwo(inputInt);
if (intTwo == 0) {
errorMessage();
} else {
int answer = intOne / intTwo;
}
}
//-------------------------------------------------------------------------------

String whatOperator(String inputInt) {
char Operator = inputInt.charAt(0);
if (Operator == '+' ) {
plus(inputInt);
} else if (Operator == '-' ) {
minus(inputInt);
} else if (Operator == '*' ) {
multiply(inputInt);
} else if (Operator == '/' ) {
divide(inputInt);
} else {
errorMessage();
}
}
//-------------------------------------------------------------------------------

void setup() {
Serial.begin(9600);
printWelcomeMessage();
}
//-------------------------------------------------------------------------------

void loop() {
whatOperator(inputInt);
}
``````

Moderator edit:
`</mark> <mark>[code]</mark> <mark>`

`</mark> <mark>[/code]</mark> <mark>`

Is there a reason why you are using Strings (uppercase S, objects created using the String library) rather than strings (lowercase s, zero terminated arrays of chars) ?

The former are generally not recommended for the small amount of memory available on the average Arduino board

It produces correct answers for me. I added some debug output and fixed a pile of compiler warnings where you specified a return type on a function but did not return a value.

``````String inputInt;
char Operator;
int intOne;
int intTwo;
//------------------------------------------------------------------------------

void printWelcomeMessage()
{
Serial.println("Please enter the integer values you wish to calculate:");
}
//-------------------------------------------------------------------------------

{
while (!Serial.available()) {};
Serial.println("Input available.");
//  Serial.println(input);
//  return input;
}

//-------------------------------------------------------------------------------

void errorMessage()
{
Serial.println("ERROR");
Serial.println("");
}

//-------------------------------------------------------------------------------

void getIntOne(String inputInt)
{
String stringIntOne = inputInt.substring(1, 4);
intOne = stringIntOne.toInt();
}

//-------------------------------------------------------------------------------

void getIntTwo(String inputInt)
{
String stringIntTwo = inputInt.substring(4, 7);
intTwo = stringIntTwo.toInt();
}

//-------------------------------------------------------------------------------

void plus(String inputInt)
{
getIntOne(inputInt);
getIntTwo(inputInt);
int answer = intOne + intTwo;
}
//-------------------------------------------------------------------------------

void minus(String inputInt)
{
getIntOne(inputInt);
getIntTwo(inputInt);
int answer = intOne - intTwo;
}
//-------------------------------------------------------------------------------

void multiply(String inputInt)
{
getIntOne(inputInt);
getIntTwo(inputInt);
int answer = intOne * intTwo;
}
//-------------------------------------------------------------------------------

void divide(String inputInt)
{
getIntOne(inputInt);
getIntTwo(inputInt);
if (intTwo == 0)
{
errorMessage();
}
else
{
int answer = intOne / intTwo;
}
}
//-------------------------------------------------------------------------------

void whatOperator(String inputInt)
{
char Operator = inputInt.charAt(0);

Serial.print("\'");
Serial.print(Operator);
Serial.println("\'");

if (Operator == '+' )
{
plus(inputInt);
}
else if (Operator == '-' )
{
minus(inputInt);
}
else if (Operator == '*' )
{
multiply(inputInt);
}
else if (Operator == '/' )
{
divide(inputInt);
}
else
{
errorMessage();
}
}
//-------------------------------------------------------------------------------

void setup()
{
Serial.begin(9600);
printWelcomeMessage();
}
//-------------------------------------------------------------------------------

void loop()
{
Serial.print("\"");
Serial.print(inputInt);
Serial.println("\"");
whatOperator(inputInt);
}
``````

Serial is slow; there is no guarantee that you get all characters in one go in below part of your code

``````String promptReadln() {
while (!Serial.available()) {};