Is there a way to output the number of items in an array?
I have an array that I'm trying to create, that is being filled with book titles. Its not a set amount of items in this array, just however many titles get inputted by user.
If say, during the program execution, the user inputs 3 book titles, I would like to use that number "3" somewhere else in the program.
Is there a piece of code that will allow me to do this?
***
Edited to add:
I am trying to use
int bookFound[] = {-1};
int arrSize = sizeof(bookFound) / sizeof(int);
BUT, when running the program, I enter 3 books, and it says the arrSize is 1. Always. No matter how many books I enter.
Is there a way to output the number of items in an array?
An array is always, always full. It's just memory, and no memory is blank. There's always some value in it. If you need to keep track of how many values you've put in an array yourself, you must do that for yourself. The array is just memory.
int bookFound[] = {-1};
That's an array of size one. If you put new values into it like this:
A static array has a static size. So when you print the size of bookFound, 1 will always be the size because it only has 1 item.
Additional information:
Have you ever used std::vector? Or possibly create your own wrapper for a static array?
Allowing the user to determine the size of an array at run-time requires DMA. Attaining the size of DMA'd array will always return the same value, regardless of the array's size.