Creating a "find" function in c++

Hi everyone,

I am new to this forum but was hoping someone could help me out with some code I am writing for school.

I have to write a find function that will search my array for an employee id, and let me know if the employee exists or not.

Here is my code so far:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct Employee
{
string name;
int id;
float ySalary;
};

void printEmployee(const Employee& c);
Employee readEmployee();
int findEmployee(const Employee array[],int tId,int num);


int main ()
{
const int NUM_EMP = 50;

Employee emp[NUM_EMP];
int i;

for (int i=0; i < NUM_EMP; i++)
{
emp[i]=readEmployee();

printEmployee(emp[i]);

}

}

//---------------------------------------------------------------------------

void printEmployee(const Employee& c)
{

cout << endl;
cout << "Employee info is: " << c.name;
cout << ", " << c.id;
cout << ", " << c.ySalary << endl;
cout << endl;

}

//----------------------------------------------------------------------------

Employee readEmployee()
{

Employee tempEmp;

cout << "Employee Name?: ";
cin >> tempEmp.name;

cout << "Employee ID?: ";
cin >> tempEmp.id;

cout << "Employee yearly salary? ";
cin >> tempEmp.ySalary;

return tempEmp;

}

//------------------------------------------------------------------------------

int findEmployee(const Employee array[],int tId,int num)

// The above "findEmployee" function is the one I'm having troubles with. All I have so far is the prototype. Any suggestions would be much appreciated. Thanks!
step 1:
search my array for an employee id

How? Think of how you traverse an array from what you've learned, then apply it.

step 2:
Found it. exists! Didn't find it. Does not exist!
I would specify parameters in the following order:
array name, array size, searched ID

1
2
3
4
5
6
7
8
int findEmployee( const Employee array[], int n, int id )
{
   int i = 0;

   for ( ; i < n && !( array[i].id == id ) ; i++ );

   return ( i );
} 
Last edited on
Topic archived. No new replies allowed.