Issues using if else if statements in terminal

I have been working on a terminal program that gets input from a user and runs the code under an if statement based off the input however, when I compile it I get a invaild conversion from const char to int error and when the program run's every bit of code runs at once. Any info will help .

#include <MemoryUsage.h>
int optionSelection;
String onResponse = "Testing";

void setup() {
  // put your setup code here, to run once:
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  delay(500);
  Serial.print("OS Version 0.5");
  Serial.print(" ");
  FREERAM_PRINT;
  Serial.print(" ");
}


void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("What do you want to do?");
  optionSelection = Serial.parseInt();
  if (optionSelection = "1") {
    Serial.print("Option 1");
  }
  else if (optionSelection = "2"); {
    Serial.print("Option 2");
  }
    
}

Welcome to the forum. Thanks for using code tags in your first post. Many don't

I removed the library from the code and whatever FREERAM_PRINT; is supposed to do and it compiles

Please post the full error that you get by using the "Copy error message" button in the IDE and post it here in code tags

Although I can compile your code it will not do what you think. In it you have

  optionSelection = Serial.parseInt();
  if (optionSelection = "1")

optionSelection is an int, So far, so good, but you are trying to compare it to a String instead of an int. In addition to that you have the syntax if the if wrong. use = for assignment and == for comparison

1 Like

These are the errors I got:

C:\Users\************\Desktop\OS\OS.ino: In function 'void loop()':

C:\Users\************\Desktop\OS\OS.ino:21:23: warning: invalid conversion from 'const char*' to 'int' [-fpermissive]

   if (optionSelection = "1") {

                       ^

C:\Users\************\Desktop\OS\OS.ino:24:28: warning: invalid conversion from 'const char*' to 'int' [-fpermissive]

   else if (optionSelection = "2"); {

                            ^

See my comment about the data type that you are testing and test for an int instead

By the way, those are warnings not errors that you are getting

1 Like

Thank you!

Just to add…

Use == for comparison.
and you can make your code shorter using a switch-case statement…

… for next time!

1 Like

A issue has arisen after fixing some of the issues. When I enter a command, no matter which one I get the same response where the program prints both options at once. Here is what it looks like:

Option 1Option 2What do you want to do?
Option 1Option 2What do you want to do?

You might want to check if there is data available in the receive buffer before you read any data.

The serial monitor line endings may need to be changed to none.

The serial input basics tutorial may be of interest.

If you make a change to your code post your latest version of your code, please.

Hello assortedkingdede
Try this sketch a propsal for a small HMI.

#include <MemoryUsage.h>
int optionSelection;
String onResponse = "Testing";

void setup() {
  // put your setup code here, to run once:
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  delay(500);
  Serial.print("OS Version 0.5");
  Serial.print(" ");
  FREERAM_PRINT;
  Serial.println(" ");
}


void loop() {
  // put your main code here, to run repeatedly:
  static bool textOut = false;
  if (!textOut) Serial.println("What do you want to do?"), textOut = true;
  if (Serial.available()) {
    switch (Serial.read()) {
      case '1':  Serial.println("Option 1"); break;
      case '2':   Serial.println("Option 2"); break;
      case '3':   Serial.println("Option 3"); break;
      case '4':  Serial.println("Option 4"); break;
      case '5':   Serial.println("Option 5"); break;
      case '6':  Serial.println("Option 6"); break;
      case '\n': textOut = false; break;
    }
  }
  /*
    optionSelection = Serial.parseInt();
    if (optionSelection = "1") {
      Serial.print("Option 1");
    }
    else if (optionSelection = "2"); {
      Serial.print("Option 2");
    }
  */
}

Have a nice day and enjoy coding in C++.
Дайте миру шанс!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.