system
1
Hello..
lets say I have an array and I want to center the output on my 4x20 LCD.
Do I take the right path with this?
char hours[14][15] = {"eins","zwei","drei","vier","fuenf","sechs","sieben","acht","neun","zehn","elf","zwoelf","mittag","mitternacht"};
This is one of my arrays which I use on my LCD.
If I try to set up the function to get the length of each entry I get a compiler error:
In function 'void StringLength(int, int)':
error: request for member 'size' in 'hours[1]', which is of non-class type 'char [15]'
And this is the code I try to use:
pos = (20-hours[1].size())/2;
Where did I go wrong?
Thanks for any help!
-andreas
mem
2
Hi Andreas, because your string lengths vary, its much more efficient to make an array of string pointers.
char *hours[] = {"eins","zwei","drei","vier","fuenf","sechs","sieben","acht","neun","zehn","elf","zwoelf","mittag","mitternacht"};
usage:
print(hours[0]); // will print eins
print(hours[1]); // will print zwei
//to get the position of zwei:
int pos = strlen(hours[1]) /2; // strlen returns the length of the string
system
3
duh! I thought too much about c++ Thanks for the hint!
PS: works like a charm!
int pos = strlen(hours[1]) /2; // strlen returns the length of the string
I'm a little confused by this. What is the purpose of "pos" exactly?
Mikal
system
5
assuming I have a LCD Display (4x20) and I need to center the text messages on each row.. that's why I do the following
"20 minus the length of my string and then divide by 2 equals the starting position of the string"
int pos = (20-strlen(hours[count]))/2;
lcd.cursorTo(2, pos);
lcd.printIn(hours[count]);
-andreas