Cannot get simple programme working

I am just a beginner i am stuck with getting my if statements working, they both execute, i know their are better way to write the programme , but i would like to know what do i have to change to make it work, i think i am getting confused on strings and char.
any help would be appreciated.

#include <LiquidCrystal.h>
#include <String.h>
int rs=7;
int en=8;
int d4=9;
int d5=10;
int d6=11;
int d7=12;
int num1;
int num2;
char choice;
int add;
int subtract;
int answer;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
lcd.begin(16,2);

}

void loop() {
// put your main code here, to run repeatedly:

lcd.setCursor(0,0);
Serial.println("enter a number 1");
lcd.println("enter num 1");
delay(2000);
while (Serial.available()==0){
}
num1=Serial.parseInt();
lcd.setCursor(0,1);
lcd.print(num1);
Serial.println(" Enter number 2");
while (Serial.available()==0){
}
num2=Serial.parseInt();
lcd.setCursor(3,1);
lcd.print(num2);
Serial.println("Do you want to add,subtract,divide,multiply");

while (Serial.available()==0){
}

choice=Serial.parseInt();

if (strcmp(choice,add)==0){
lcd.setCursor(1,1);
lcd.print("+");

     answer=(num1+num2);
     lcd.setCursor(5,1);
     lcd.print(answer);
     lcd.setCursor(4,1);
     lcd.print("=");
     delay(5000);

}
if (strcmp(choice,subtract)==0){
lcd.setCursor(1,1);
lcd.print("-");

     answer=(num1-num2);
     lcd.setCursor(5,1);
     lcd.print(answer);
     delay(5000);

}

}

type or paste code here

Please use code tags <CODE> to post you code.
As you can see it got scrambled a bit

1 Like

Welcome to the forum

char choice;

choice is a single character char variable

choice=Serial.parseInt();

However, you assign an int to it.

if (strcmp(choice,add)==0){

then use the strcmp() function to test its value but strcmp() only works with C style zero terminated strings

Decide on a data type for choice (and other variables) and use the same data type throughout the sketch

Lets start with the first if statement: If you read the forum guidelines you would have pasted your code using code tags. How to get the best out of this forum

Using the String class can cause memory problems. See the Evils of Arduino Strings page.

Your method of user input is blocking. If you want to be able to do other things while waiting for the user to respond, chapter 7 of Robin2's Planning and Implementing an Arduino Program tutorial shows how.

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