I tried to find the index of an element in an array....i managed to make it work with ints using the following function:
int *getIndexOfInt(int *arr, int size, int check) {
int *result;
int *k;
int count = 0;
for (int i = 0; i <= size - 1; i++) {
if (arr[i] == check) {
k[count] = i;
count++;
}
}
if (count > 0) {
*result = *k;
return result;
} else
cout << "Not Found";
}
However when i tried this for strings it just gave me errors (program exited with status 11) or infinite loops:
int *getIndexOfString(string *arr, int size, string check) {
int *result;
int *k;
int count = 0;
for (int i = 0; i <= size - 1; i++) {
if (arr[i] == check) {
k[count] = i;
count++;
}
}
if (count > 0) {
*result = *k;
return result;
}
else cout << "Not Found";
}
Can you please tell me why and maybe help me fix the errors?
EDIT: The result variable is the array which is then used in the main function,it contains the indexes where the string was found in the given array. The k variable is just an array in which values are stored before being added into the result. The arr is the given string array the size is the givven size and the check is the string which the code will search for.
int* kandint* resultis never initialized.std::find.std::vector. If you're not usingstd::find, you're not using an important part of C++; if you're not usingstd::vector, you're using parts of C++ which are broken (because of C compatibility).