Linear search for strings in an Array

I am trying to find names in sequential text file. I want to find the name, if it exists, and do something. If the name does not exist I want to do something different.

I want to make the search as a part of a function so I can call it at different times.



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
int enterChoice()
        {
            int menuChoice;
            cout << "Payroll Maintenance Program Main Menu\n\n"
                << "Choose an option\n\n"
                << "1. Add a Record\n"
                << "2. Display a Record\n"
                << "3. Display the File\n"
                << "4. Modify a Record\n"
                << "5. Delete a Record\n"
                << "6. Exit Program\n\n"
                << "Enter your selection (1–6) " << endl;
            cin >> menuChoice;
            return menuChoice;
        };// End enterChoice function.


void search(string empLastName[], string searchName, int count )
{
        cout << "Please enter a name or END to terminate:" <<  endl;
        cin >> searchName;
          
        for (int j = 0; j < 10; ++j )
        {
        if (empLastName[j] == searchName)
        {
            cout << j << endl;
        }
        else 
        {
            cout << "didn't find it" << endl;
        }
        }
};


Just for simplicity sake I am only including the sections of my program that I think are pertinent.

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
int main(int argc,
         const char * argv[])
{
  
   //
  string searchName;
    
    
  // Number of items in the array.
  int count = 0;
  
  // Declare Array Variables for input by user
  string empLastName[SIZE] = {""};
  char   payrollType[SIZE] = {0};
  double hoursWorked[SIZE] = {0};
  double payRate[SIZE] = {0};
  int    unionCode[SIZE] = {0};
  
  // Declare Array Variables calculated
  double regularPay[SIZE];
  double overtimePay[SIZE];
  double grossPay[SIZE];
  double stateTax[SIZE];
  double federalTax[SIZE];
  double unionDues[SIZE];
  double netPay[SIZE];
    
    
    // switch statement to ask for and recieve users choice from a menu
    
    int menuChoice;
    while((menuChoice = enterChoice())!= 6 ) //loop while op does not = 6
    {
        switch(menuChoice) //will only execute if op does not equal 6
               {
                case 1:
                               getInput( empLastName,
                                        payrollType,
                                        hoursWorked,
                                        payRate,
                                        unionCode,
                                        count );
                       
                               getUserInput(empLastName,
                                            payrollType,
                                            hoursWorked,
                                            payRate,
                                            unionCode,
                                            count );
                       
                       cout << "Press anykey to continue.";
                       system("clear");
                       cin.ignore();
                       cin.get();
                       
                       break; //so we dont go through all cases
                case 2:
                       getInput( empLastName,
                                payrollType,
                                hoursWorked,
                                payRate,
                                unionCode,
                                count );
                       
                       
                       search(empLastName,
                              searchName,
                              count);
                       
                                          
                       
                      
                       
                       
                       
                       
                       
                       
                      
//                       break;

//                case 3:
//                            
//                    break;
// 
The ; on lines 15 and 34 are not necessary.

To compare strings use a function like:
1
2
3
4
if( searchName.compare(empLastName[j]) == 0 )
{
    cout << "name found !";
}
Thanks for the reply soranz. I have added your code to my project in the void search function. My project does not work. I am still looking for how to implement it.

-Josh
Topic archived. No new replies allowed.