Choosing a string from array of strings

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?

I don't really understand your intention, do you mind giving an actual case as an example? like what are the input and output?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main(){
	std::string arrayOfStr[] = {"Jan", "Feb", "Mar", "Apr"};
	std::string inputStr;

	std::cin >> inputStr;

	int arraySize = sizeof(arrayOfStr) / sizeof(std::string);

	for(int a = 0; a < arraySize; a++){
		if(arrayOfStr[a] == inputStr){
			//string exists
		}
	}
}
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?

edit: Can you not use size, arrayOfStr.size ?
Last edited on
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.

As for the division, we can look at it this way:

Array of 10 apples = A
1 apple = 2 bytes

Let's assume that 1 apple is equal to 2 bytes.

1
2
sizeof(apple) = 2 bytes
sizeof(A) = 10 * sizeof(apple) = 10 * 2 = 20 bytes


to get the number of apples in array of 10 apples, we just have to divide them right?

so we get:
sizeOfArray = sizeof(A) / sizeof(apple)

similar to our division before:
sizeOfArray = sizeof(arrayOfStr) / sizeof(std::string)

Also if you don't mind some changes, we can use std::find()
http://www.cplusplus.com/reference/algorithm/find/

Nice. Excellent explanation. Thanks!
A better way in the C++11 standard for this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>

int main()
{
        std::string months[] = { "Jan", "Feb", "Mar", "Apr" };

        std::string str;
        std::cin >> str;

        bool exists = false;

        for (const std::string &s : months)
        {
                if (str == c)
                {
                        exists = true;
                        break;
                }
        }

        std::cout << (exists ? "exists" : "doesn\'t exist") << '\n';

        return 0;
}

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.
Last edited on
Note you can write

int arraySize = sizeof(arrayOfStr) / sizeof(arrayOfString[0]);

that is, the count of elements in the array is the size (in bytes) of the whole array divided by the size of its first element.

instead of

int arraySize = sizeof(arrayOfStr) / sizeof(std::string);

If you avoid the type, which will differ from array to array, then you can write a macro to calculate the count, e.g.

1
2
3
#define COUNT_OF(A) sizeof(A) / sizeof(A[0])

int arraySize = COUNT_OF(arrayOfStr);


which can tidy your code up a bit.

If you're using Microsoft Visual C++, there is a macro provided by the CRT headers: _countof()

int arraySize = _countof(arrayOfStr);

Andy
Last edited on
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.
Last edited on
Topic archived. No new replies allowed.