passing arrays into functions

Hello forum.

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.

Thanks for taking time out and helping me out.
I hope I am not doing your homework lol but this is what I got working for you.


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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//Array passer

#include <iostream>
#include <string>

using namespace 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;

    const int 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;
}



Thank you for the example.


This is what I have programmed so far:






// array into function.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

void userInputArray(string[],int);


int _tmain(int argc, _TCHAR* argv[])
{

string firstName[] =

{"Jim","Tuyet","Ann","Roberto","Crystal","Valla","Mathlida"};





userInputArray(firstName,7);



system("pause");
return 0;
}


void userInputArray(string arr[],int size){

for(int iii = 0; iii <1; iii++)
{
string name;
cout << "Enter first name: ";
cin >> name;

if (find( arr, arr + 7, name) != arr + 7) {
cout << iii << endl;

}else{ cout << "7" << endl;
}
}
}

I need the elements to correspond with their place in the array index.

again thank you for any help, hints, and advice.
Topic archived. No new replies allowed.