[SOLVED] invalid conversion from 'char' to 'const char*' [-fpermissive]

Hello, I'm having this error (I will transcribe full at the end) when I compile the following code...
It is pretty simple... I have a 3x4 keypad and an LCD display with I2C communication.
When I press a key, it is displayed on the LCD.
I need to convert the char given by the keypad to int, so I can work with these numbers (actually, I need to make a single number with every key I press until I press '*' or '#').
The problem is in the function atoi(), but I don't know how to solve it...

#include <Keypad.h>
#include <Wire.h>                 
#include <LiquidCrystal_I2C.h>   
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);//LCD adress

int cursor=0;
int n=0;
char keyy;
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {13, 12, 11, 9};  //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6};     //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


void setup(){ 
  lcd.begin(16,2);                  //Display initialization  
  Serial.begin(9600);
}

void loop(){
  

  lcd.setCursor(0,0);               //Locate cursor
  lcd.write("Ingresar numero");
  
  char key=keypad.getKey();         //Get keypad number
  lcd.setCursor(cursor,1);
  if(key){                          //If key is pressed, write it on display and move cursor
    if(key!='#'&&key!='*'){         //Write only numbers
      lcd.write(key);
      Serial.print(key, DEC);            //Verification line. Not necessary when code is ready
      n=atoi(key);
      Serial.println(n, DEC);

    }
    cursor++;
  }
  if(key=='#'){                     //Key # is data end.
    cursor=0;
    lcd.setCursor(cursor,1);        //Relocate cursor
    lcd.clear();                      //Clear display
  }
}

If the character is '0' through '9' you can convert it to an integer by subtracting the character '0'. That is how it is usually done.

You are not accumulating digits. You either need to save the characters in a buffer and convert the buffer to an integer using atoi() or convert each digit and add it to the previous digits. Check out some of the keypad examples for how other people have done it.

The atoi() function expects a null-terminate char array as its argument, not a single char. You can convert a single character digit to a numeric with:

num = key - '0';

If the digit the user entered was, say, a '5', the ASCII code is 53. Since the ASCII code for zero is 48:

num = 53 - 48;
num = 5;

This would allow you to avoid a string array and just use key instead.

econjack:
The atoi() function expects a null-terminate char array as its argument, not a single char. You can convert a single character digit to a numeric with:

num = key - '0';

If the digit the user entered was, say, a '5', the ASCII code is 53. Since the ASCII code for zero is 48:

num = 53 - 48;
num = 5;

This would allow you to avoid a string array and just use key instead.

Well, this was really useful, and made understand how atoi works. Thanks a lot for your reply, econjack!

johnwasser:
If the character is '0' through '9' you can convert it to an integer by subtracting the character '0'. That is how it is usually done.

You are not accumulating digits. You either need to save the characters in a buffer and convert the buffer to an integer using atoi() or convert each digit and add it to the previous digits. Check out some of the keypad examples for how other people have done it.

Also thank you, johnwasser. I know I'm not accumulating digits, it'll be the next step now I have the chars converted to ints.
As I need to make a full number, I'll just multiply by 10 and add them to the previous numbers.

Again, thanks to both. This problem's been solved in a really simple way.

And I even think you can simplify it by using

char keys[ROWS][COLS] = {
  {1, 2, 3},
  {4, 5, 6},
  {7, 8, 9},
  {'*', 0,'#'}
};

(subtle difference)

But you have to remember now the numbers are really numbers. So if you want to make a string with it you have to make ASCII numbers from them again before putting it into a string. But that's as simple as

char numberAsChar = number0to9 + '0';

But don't use that on numbers of more then 1 digit :smiley: But as long as you do that with the single digits from the above keyboard you are fine. And you don't have to do it if you want to print that number to Serial or a LCD but be sure to use a byte (not a char) as the type in which you save the number or cast it to a byte if you do use a char.

It's like the other way around then you try to do know which seems more logical because you are really interested in the numbers :slight_smile:

Thanks, septillion.
It would be more useful if I worked only with numbers. As I need to use the LCD to show them, I need the chars. I found that simple way to use them: just substract '0' and make the operations needed.

santamaria14:
As I need to use the LCD to show them, I need the chars.

You missed a part of the answer... :wink:

septillion:
And you don't have to do it if you want to print that number to Serial or a LCD but be sure to use a byte (not a char) as the type in which you save the number or cast it to a byte if you do use a char.

lcd.print(1) works also fine doesn't it? :wink:

septillion:
You missed a part of the answer... :wink:
lcd.print(1) works also fine doesn't it? :wink:

Well, I haven't understood that part of the answer. You're completely right, and just made my code simplier. Thanks a lot!

septillion:
And I even think you can simplify it by using

char keys[ROWS][COLS] = {

{1, 2, 3},
 {4, 5, 6},
 {7, 8, 9},
 {'*', 0,'#'}
};



(subtle difference)

This has the tiny problem that NO_KEY is equal to 0, so I'd keep the quotes.

Bummer :confused: Always liked -1 or 255 more as a no-action return value. You can also change 0 to 10 but yeah, the extra code you then need defeats the purpose a bit :stuck_out_tongue:

hi guys
i have the same problem here "invalid conversion from 'char*' to 'char' fpermessie"
and here is my code

float recherche(char code,float val) {
char ptr=buffer; // start at the beginning of buffer
while((long)ptr > 1 && (long)ptr < (long)buffer+taille) { // walk to the end
if(ptr==code) { // if you find code on your walk,
return atof(ptr+1); // convert the digits that follow into a float and return it
}
ptr=strchr(ptr,' ')+1; // take a step from here to the letter after the next space
}
return val; // end reached, nothing found, return default val.
}
The problem is in the function strchr(), but I don't know how to solve it...

Please edit your post to include code tags. While you're at it, also post all of at least a compilable example, we're not [url=http://snippets-r-us.com/Snippets R Us[/url]. And post EXACTLY what you expect it to do.

Next, I already see A LOT of weird stuff. Why call something a pointer that is not a pointer? What cast that to a long? etc...