# Else if problem

Im having trouble with craeting a simple calculator that can perform the 4 basic operations. The mistake must be with my if-else statements. I have 4 conditions addition subtraction multiplication and division, which are associated with the 4 letters (A, B, C and D) on the keypad in that order. So if a is pressed the two numbers that will be inputed by the user will be added if b is they will be subtracted and so on. For a (addition) i used if and for all the others i used else if. And for the same test (==) for all. Now, for the proble, no matter what key is pressed it is always addition that runs. I dont know what the problem might be. I was thinkin that maybe the same test cannot be ran with all the statements, this might be right but there are not meny other tests i could use [I could do if (input == ‘A’) {call addition} and then else if (input != A) && (input != A) && (input != A){call subtraction}- to do == B but what would i do for ==C and == D] but i still dont understand why cant i run the same test as many times as i want?

I’ve been trying to figure this out for the past two days. Any help would be greatly appreciated.

`````` #include <Keypad.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd (7,8,9,10,11,12);

int WelcomeText(){
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
lcd.write("Hello");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.write("I am a calcuator");
delay(1750);
lcd.clear();
delay(1000);
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
lcd.write("Press...");
delay(750);
lcd.clear();
lcd.setCursor(0,0);
delay(500);
lcd.clear();
lcd.setCursor(0,0);
lcd.write("'B' to subtract");
delay(500);
lcd.clear();
lcd.setCursor(0,0);
lcd.write("'C' to multiply");
delay(500);
lcd.clear();
lcd.setCursor(0,0);
lcd.write("'D' to divide");
delay(500);
lcd.clear();
lcd.setCursor(0,0);
lcd.write("Then...");
delay(500);
lcd.clear();
lcd.setCursor(0,0);
lcd.write("Enter 2 numbers");
delay(600);

};

lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(x, DEC);
lcd.setCursor(2,0);
lcd.write("+");
lcd.setCursor(4,0);
lcd.print(y, DEC);
lcd.setCursor(6,0);
lcd.write("=");
int z;
z = x+y;
lcd.setCursor(8,0);
lcd.print(z,DEC);
}

int Subtraction (int x, int y){
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(x, DEC);
lcd.setCursor(2,0);
lcd.write("-");
lcd.setCursor(4,0);
lcd.print(y, DEC);
lcd.setCursor(6,0);
lcd.write("=");
int z;
z = x-y;
lcd.setCursor(8,0);
lcd.print(z,DEC);
}

int Multiplication (int x, int y){
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(x, DEC);
lcd.setCursor(2,0);
lcd.write("*");
lcd.setCursor(4,0);
lcd.print(y, DEC);
lcd.setCursor(6,0);
lcd.write("=");
int z;
z = x*y;
lcd.setCursor(8,0);
lcd.print(z,DEC);
}

int Division (int x, int y){
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(x, DEC);
lcd.setCursor(2,0);
lcd.write("/");
lcd.setCursor(4,0);
lcd.print(y, DEC);
lcd.setCursor(6,0);
lcd.write("=");
int z;
z = x/y;
lcd.setCursor(8,0);
lcd.print(z,DEC);
}

void setup() {

WelcomeText();

}

const byte rows =4;
const byte cols =4;

char keys[rows][cols]={
{1,2,3,'A'},
{4,5,6,'B'},
{7,8,9,'C'},
{'*',0,'#','D'}
};

byte rowPins[rows] = {0,1,2,3};
byte colPins[cols] = {4,5,6,13};

void loop(){

int a;
int b;
int Operator;

while (Operator != NO_KEY) {

if (Operator == 'A'){
if (a != NO_KEY){
do {
}while (b == NO_KEY);
delay(100);
}
}

else if (Operator == 'B'){
if (a != NO_KEY){
do {
}while (b == NO_KEY);
Subtraction(a,b);
delay(100);
}
}

else if (Operator == 'C'){
if (a != NO_KEY){
do {
}while (b == NO_KEY);
Multiplication(a,b);
delay(100);
}
}

else if (Operator == 'D'){
if (a != NO_KEY){
do {
}while (b == NO_KEY);
Division(a,b);
delay(100);
}
}

else{
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
lcd.write("Invalid Operator");
}

}

}
``````

Post your code here (within code tags), pointing outside will discourage many willing to help...

Check your code for the second if, you are missing 'B' in the code...

``````     else if (Operator == ){
``````

Is this the same project? Please don't cross-post.

raymondogunjimi: I'm trying to create an adder. The user would have to enter two numbers on the keypad and their sum would be displayed on the LCD. For some reason it doesn't work. Any advice would be much appreciated.

No that is my first project, i was only trying to make a calculator that only could perform one operation (adding) this is totally different…much more challenging to me as well

This isn't going to work:

``````while (Operator != NO_KEY) {
if (Operator == 'A') {
if (a != NO_KEY) {
do {
} while (b == NO_KEY);
delay(100);
}
}
``````

Almost certainly you will get NO_KEY for "a" and "b". You need to wait until a key is pressed. Conceivably you could make a function to do that:

``````int waitForKey ()
{
int k;
do
{
} while (k == NO_KEY);
return k;
}  // end of waitForKey
``````

``````while (Operator != NO_KEY) {
if (Operator == 'A') {
a = waitForKey ();
b = waitForKey ();
delay(100);
}
}
``````
`````` Operator = keypad.getKey();

while (Operator != NO_KEY) {
...
}
``````

Now, for the problem, no matter what key is pressed it is always addition that runs.

You don't re-acquire "Operator" and since you have a "while" loop, once you got an "A" once you will always have it. Change "while" to "if":

`````` Operator = keypad.getKey();

if (Operator != NO_KEY) {
...
}
``````

Yea i get that but that is not my problem. In fact i already have a while look for be and an if for az. Besides i dont want to re run the whole calculation every times an answer is displayed. My problem is that the program does not recognize my else if statement. It simply jumps to else if my first if condition is false or runs my firs function if it is true but it DOES NOT CONSIDER MY IF ELSE STATEMENTS.

If you pick A first you'll be stuck with Operator == 'A'. You can't pick a second time with this code. Since Operator is local to loop and not initialized, it might just be starting out with an A in it before you ever touch a key.

Sprinkle some statements in there to print Operator and I think you'll see what is happening.

In fact i already have a while look for be and an if for az

What?

raymondogunjimi: but it DOES NOT CONSIDER MY IF ELSE STATEMENTS.

No, you have an error in your logic. Do you really think the compiler would have lasted this long if if/else did not work? Stop blaming the compiler and start listening to people who are trying to help you.