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.

Your code makes no attempt to parse the "arrows". Is your plan to add support later?

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:");
}
//-------------------------------------------------------------------------------

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

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

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;
  Serial.println(answer);
}
//-------------------------------------------------------------------------------

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

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

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

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() {
  inputInt = promptReadln();
  whatOperator(inputInt);
}

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

</mark> <mark>[/code]</mark> <mark>
tags added.

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:");
}
//-------------------------------------------------------------------------------


String promptReadln()
{
  while (!Serial.available()) {};
  Serial.println("Input available.");
//  String input =  Serial.readString();
//  Serial.print("Input received: ");
//  Serial.println(input);
//  return input;
  return Serial.readString();
}


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


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;
  Serial.println(answer);
}
//-------------------------------------------------------------------------------


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


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


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


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()
{
  inputInt = promptReadln();
  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()) {};
  return Serial.readString();
}

You can read the updated Serial Input Basics thread to get ideas how to approach the reading.

Thank you John!

Whats weird was that the exact same code worked for me last night, but wasn't working at all today. I appreciate the help! :slight_smile:

Gwydion0412:
Whats weird was that the exact same code worked for me last night, but wasn't working at all today.

It didn't work for me when I had the Serial Monitor baud rate set wrong. :slight_smile: