This project includes liquid crystal and keypad. Please help me. Thanks a lot!

Hi. I'm trying to make a calculator, but I ran into some problem. My code is rly bad as I'm new to this. Thanks for stopping by and helping me!
Here is my code:

// Why
// am
// I
// getting 
// these 
// error 
// messages?
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <stdlib.h>
const byte ROWS = 4;
const byte COLS = 4;
char keys [ROWS][COLS] = 
{
  {'1','2','3','/'},
  {'4','5','6','*'},
  {'7','8','9','-'},
  {'~','0','=','+'}
};
byte rowPins[ROWS] = {7,6,5,4};
byte colPins[COLS] = {3,2,1,0};
Keypad keypad = Keypad(makeKeymap(keys),rowPins,colPins,ROWS,COLS);
LiquidCrystal lcd(12, 13, 8, 9, 10, 11); 
void setup()
{
  lcd.begin(16,2); 
}
struct Rational
{
  int numerator;
  int denominator;
};
typedef struct Rational rational;
rational div(rational x, int y) 
{
  rational z =
  {x.numerator, x.denominator * y};
  return z;
}
rational mul(rational x, int y) 
{
  rational z =
  {x.numerator * y, x.denominator};
  return z;
}
rational sub(rational x, rational y) 
{
  rational z = 
  {
    x.numerator * y.denominator - y.numerator * x.denominator,
    x.denominator * y.denominator
  };
  return z;
}
rational add(rational x, rational y) 
{
  rational z =
  {
    x.numerator * y.denominator + y.numerator * x.denominator,
    x.denominator * y.denominator
  };
  return z;
}
rational calc(String num)
{
  String temp = "";
  String ari = "";
  rational a = {0, 1};
  bool cal = false;
  int length = 0;
  String S = num;
  for (int i = 0; i < sizeof(num)/sizeof(num[0]); i++)
  {
    if (num[i] == '/')
    {
      if (ari == "/")
      {
      	temp = S.substring(0, i - length + 1);
        a = div(a, temp.toInt());
        S.remove(0, i-length);
        ari = "/";
        cal = true;
        break;
      }
      if (ari == "*")
      {
      	temp = num.substring(0, i - length + 1);
        a = mul(a, temp.toInt());
        S.remove(0, i-length);
        ari = "/";
        cal = true;
        break;
      }
      if (ari == "")
      {
      	temp = num.substring(0, i+1);
        a.numerator = temp.toInt();
      	S.remove(0, i-length);
        length = temp.length()-'0';
        ari = "/";
        break;
      }
    }
    if (num[i] == '*')
    {
      if (ari == "/")
      {
      	temp = num.substring(0, i - length + 1);
        a = div(a, temp.toInt());
        S.remove(0, i-length);
        ari = "*";
        cal = true;
        break;
      }
      if (ari == "*")
      {
      	temp = num.substring(0, i - length + 1);
        a = mul(a, temp.toInt());
        S.remove(0, i-length);
        ari = "*";
        cal = true;
        break;
      }
      if (ari == "")
      {
      	temp = num.substring(0, i+1);
        a.numerator = temp.toInt();
      	S.remove(0, i-length);
        length = temp.length()-'0';
        ari = "*";
        break;
      }
    }
  }
  if (cal == false)
    a.numerator = num.toInt();
  return a;
}
int gcd(int x, int y)
{
  if(y == 0)
    return x;
  else
    return gcd(y, y % x); 
}
void reduce(rational &x) 
{
  int GCD = gcd(x.numerator, x.denominator);
  x = {x.numerator / GCD, x.denominator / GCD};
}
int x = -1;
String number = "";
String AriTemp = "";
int count = 0;
rational NumTemp = {0, 0};
rational FinResult = {0, 0};
double DecFinResult = 0;
void loop()
{
  char key = keypad.getKey();
  if (key != NO_KEY)
  {
    x += 1;
    number = number + key;
    count += 1;
    lcd.setCursor(x,1); 
 	lcd.print(key); 
    if (x == 16)
    {
      lcd.clear();
  	  x = 0;
  	  lcd.setCursor(x,1);
    }
    if (key == '+')
    {
      AriTemp += "+";
      if (AriTemp.length() == '2')
      {
        if(AriTemp[0] == '+')
        {
          NumTemp = add(NumTemp, calc(number));
          AriTemp.remove(0, 0);
        }
        else if(AriTemp[0] == '-')
        {
          NumTemp = sub(NumTemp, calc(number));
          AriTemp.remove(0, 0);
        }
      }
	  else
      {
        NumTemp = calc(number);
      	AriTemp += "+";
      }
      number = "";
  	}
    if (key == '-')
    {
      AriTemp += "-";
      if (AriTemp.length() == '2')
      {
        if(AriTemp[0] == '+')
        {
          NumTemp = add(NumTemp, calc(number));
          AriTemp.remove(0, 0);
        }
        else if(AriTemp[0] == '-')
        {
          NumTemp = sub(NumTemp, calc(number));
          AriTemp.remove(0, 0);
        }
      }
	  else
      {
        NumTemp = calc(number);
      	AriTemp += "-";
      }
      number = "";
  	}
    delay(10);
  }
  if (key == '=')
  {
    lcd.clear();
  	lcd.setCursor(0,0);
    if (NumTemp.denominator == 0)
      FinResult = calc(number);
    else if (AriTemp[0] == '+')
      FinResult = add(NumTemp, calc(number)); 
    else if (AriTemp[0] == '-')
      FinResult = sub(NumTemp, calc(number)); 
    reduce(FinResult);
    DecFinResult = FinResult.numerator / FinResult.denominator; 
    lcd.print(DecFinResult);
    lcd.setCursor(0,1);
    lcd.print(FinResult.numerator);
    lcd.print("/");
    lcd.print(FinResult.denominator);
  } 
}
2:1: error: 'rational' does not name a type; did you mean 'atol'?



3:1: error: 'rational' does not name a type; did you mean 'atol'?



4:1: error: 'rational' does not name a type; did you mean 'atol'?



5:1: error: 'rational' does not name a type; did you mean 'atol'?



6:1: error: 'rational' does not name a type; did you mean 'atol'?



8:13: error: variable or field 'reduce' declared void


8:13: error: 'rational' was not declared in this scope
8:13: note: suggested alternative: 'atol'



8:23: error: 'x' was not declared in this scope











avre[0m   1.8.6   e[90m/home/tcad/.arduino15/packages/arduino/hardware/avr/1.8.6e[0m
 exit status 1


(This part should be correct. )

Welcome to the forum

You are getting those error messages because, as they say, rational does not name a type

What do you think rational is and where is it declared in the sketch ?

I think of rational as a type for rational numbers, which has a numerator and a denominator.
I suppose it should be declared outside of everything, shouldn't it?

You could declare it as a global variable outside of any function, but what C++ valid data type will you declare it as ?

string? double? int?
I don't really know cause it all seems a bit off.

In your sketch, rational is an instance of a struct named Rational but you are trying to use it as a data type in function definitions, hence the errors

Turn on Verbose, you will get more info.

Didn't struct create a new data type and typedef gives it a new name? Why couldn't it be used in function declarations?

You can use a struct as a data type but if I remember correctly it has to be the struct definition, not the struct instance

The question must have arisen before on the forum because I found this in my forum examples folder

struct Person
{
    String first_name;
    String last_name;
    int age;
    float salary;
};

void setup()
{
    Serial.begin(115200);
    Person p = get_data();
    display_data(p);
}

void loop()
{
}

Person get_data()
{
    String first_name = "Fred";
    String last_name = "Bloggs";
    int age = 99;
    float salary = 123.45;
    return Person{ first_name, last_name, age, salary };
}

void display_data(const Person& p)
{
    Serial.print("First Name: ");
    Serial.println(p.first_name);
    Serial.print("Last Name: ");
    Serial.println(p.last_name);
    Serial.print("Age: ");
    Serial.println(p.age);
    Serial.print("Salary: ");
    Serial.println(p.salary);
}
1 Like

So using what I typedef as function definition is illegal?

I think the problem is that you are using the name of the struct instance as the function data type rather than the name of the struct that you defined

Compare/contrast the example that I posted with your code to see what I mean

1 Like

I changed to this and it now compiles, just have to deal with the algorithm itself now.

struct rational
{
  int numerator;
  int denominator;
};
struct rational div(rational x, int y)
{
  struct rational z =
  {x.numerator, x.denominator * y};
  return z;
}

Thanks for your help! Greatly appreciated.

Getting a sketch to compile is always a satisfying step. At least it then gives you the chance to look for bugs

1 Like

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