I am having trouble figuring out a way to select a string from user input. I's like to check an array and see if the string exist. Such as an array with the name of months. Can someone point me in the right direction?
This works. I Just wanted to have the user pick a month and output if it exists or not.
Could you explain: int arraySize = sizeof(arrayOfStr) / sizeof(std::string);
I know that your using arraySize as the limit of the loop iteration, but I would have thought sizeof(arrayOfStr) would be enough. What are you dividing here?
std::string is actually a struct that contains a pointer to allocate it's memory to store the string.
Since the struct contains only the pointer and not the allocated character array that is used to store the string, sizeof operator will return only the size of the struct(the pointer and some other std::string data) in bytes.
A range-based for loop is preferred over using a regular for loop and using the subscript operators while manually controlling the iteration. It is less error prone.
Of course, to make it more efficient you can use a binary search instead of iterating through all the elements. But, it wouldn't make much of a difference with only 4 elements.
If you use a ranged-based for loop you do not need to know the size of the array.
Also, you should use functions instead of macros. Macros are dangerous and reduce readability. Although, it's harder creating a function to get the size of an array instead of a macro. You can always make the function inline. Except it isn't guaranteed the compiler will make it inline.
That's what range-based for loops are for. If the size of the array needs to be found then you should be using vectors instead. Though if you're using an array then you should know the size already... unless you dynamically create it. Which is again vectors.