You’ve got several problems here.
Firstly you might need to use
std::string instead of just
string comes from the C++ standard library, in the
I don’t think
Pokitto::Display::print accepts a
std::string, I think you have to use a
const char * and
c_str() is how you get one of those from a
If your strings are constant, I’d suggest you use
const char * instead.
std::strings are useful but they’re also a bit ‘heavier’.
Lastly, I’m going to have to explain the inconvinient truth about passing arrays to functions.
When an array is passed to a function, it is no longer an array, it becomes a pointer to the first element of the array. This is because of the rules of C that C++ inherited. As such,
sizeof(menuItems) will give you the size of a pointer, not the size of your array.
The common solution to this is to pass the size separately.
So eseentially your function would become
int displayMenu(std::string menuItems, std::size_t size) (I’ll explain the
size_t thing in a moment).
You would call it like this:
displayMenu(menuItems, sizeof(menuItems) / sizeof(menuItems));
I know what you’re thinking - “That’s going to be tedious”.
There’s a way to make it less tedious.
There is a way to make it less tedious, but if I show you that way, I most likely won’t be able to explain it to you.
Not because I don’t know how it works, but because it relies on one of the most advanced features of C++ - templates.
Some other tips:
It looks like your return value isn’t doing anything, so you could mark your function
void and not bother with a return value.
And when indexing an array, you should use
std::size_t instead of
std::size_t is unsigned (as opposed to
int which is signed) and it’s guaranteed to be large enough to express the size of any array on any platform. It’s also the type given out by
What your code probably should look like (untested).
void displayMenu(std::string menuItems, std::size_t size)
int fontPositionY = 25;
for (std::size_t i = 0; i < size; i++)
fontPositionY += 20;