I need help with passing an array into a function. I write a function that passes in a single name and the array into a function and have it search the array to see if the name is in the array. If found it will return the array index where the name is found, else it returns the number 7.
I can seem to find a way to create this function. I would really appreciate any help, hints and advice.
//Array passer
#include <iostream>
#include <string>
usingnamespace std;
class Test
{
public:
int searchArray(const string toCheck[], string toFind);
};
int Test::searchArray(const string toCheck[], string toFind)
{
//Sequential search algorithm
for (int i = 0; i < 3; i++)
{
if (toCheck[i].find(toFind) != string::npos)
return i;
}
//If it is never found in the foor loop this will be returned
//Why use 7? LOL
return 7;
}
int main()
{
Test tester;
constint MAX_SIZE = 3;
string arrayToPass[MAX_SIZE];
arrayToPass[0] = "testZero";
arrayToPass[1] = "testOne";
arrayToPass[2] = "testTwo";
//Calling the search function
//First parameter passes the array I created here in Main()
//Second parameter pass the string we are looking for
cout << tester.searchArray(arrayToPass, "testOne") << endl; //This will return 1, because it is there
cout << endl;
cout << endl;
cout << tester.searchArray(arrayToPass, "Not here") << endl; //This will return 7 because it is not there
return 0;
}