Ok, so I have an array of objects that have 3 get functions. Get last name, get first name (both strings) and get salary. What I'm trying to do is get a user to enter First name and Last name and it search the array for the persons first and last name. When it finds it I want it to display all of the info, the First name, last name and the salary. I was going to use a sequential search but I've never done it with objects using the get functions. Also, that function I want to display the contents what should I pass in? The object or something else?

Thanks,
n8thatsme

Recommended Answers

All 18 Replies

Just a guess here but you could try to use a hash table and use the last name as the key.

Well if you have a person class, then it can have your get or accessor functions.

for example:

class People{
public:
  getFirstName();
  .......
private:
  string firstname;
...
}

And you should be able to have a vector of such objects to store all your values. Then you can do a search on the vector, and call your get functions to return the values you need.

This is what I'm kind of doing, of course I can't compare the first name and last name of the EmployeeInfo with &&. So if the user enters: Tom Jones it gets put into the info variable, I need to compare that with the get_first_name and get_last_name. Or some how split up the names in the info variable. I have to use an array in this even though I would use a vector. Assume that the array of objects is filled.

#define MAX_EMPLOYEE_SIZE 100

Employee EmployeeInfo[MAX_EMPLOYEE_SIZE];
string info;

cout << "Enter employee name to find: \n";
getline(cin,info);
      for(int cntr = 0; cntr < MAX_EMPLOYEE_SIZE; cntr++)
      {
         if(info == (EmployeeInfo[cntr].get_first_name() && 
			  EmployeeInfo[cntr].get_last_name()))
         {
            cout << EmployeeInfo.display_info(); // Display info here, what to pass to function?
         }
      }

I had that working but it looped through 100 times of course, now I'm stumped. Anyone know what to do so it will stop looping after its found?

You shouldn't have to pass anything to display_info(). Since its a class member function it already has access to the data members for that object.

So you can either break out of the for loop using "break", or use a flag to signal that you have found the value you're looking for, something like this would work.

bool done = false;
int  cntr    = 0;
while(cntr < MAX_EMPLOYEE_SIZE && !done){
         if(info == (EmployeeInfo[cntr].get_first_name() && 
			  EmployeeInfo[cntr].get_last_name()))
         {
            cout << EmployeeInfo.display_info(); 
            done = true;
         }
      }
}

I still get an error for the &&. Here is the error:

Error 1 error C2676: binary '&&' : 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator c:\documents and settings\nate mcgee\my documents\visual studio 2005\projects\employee\employee\employee.cpp 80

This is not valid code. The result of the && operation is a boolean value and you cannot equate that to a string.

if(info == (EmployeeInfo[cntr].get_first_name() && 
			  EmployeeInfo[cntr].get_last_name()))

You can either split info into two strings and compare each one with first and last name respectively. Or you can concatenate the first and last name and then compare that string to info.

This goes into an infinite loop, what am I doing wrong?

bool done = false;
      int  cntr    = 0;
      string full_name;
      while(cntr < MAX_EMPLOYEE_SIZE && !done)
	  {
	     full_name = EmployeeInfo[cntr].get_first_name() + " " 
			    + EmployeeInfo[cntr].get_last_name();
         if(info == full_name)
         {
            EmployeeInfo[cntr].display_info(); 
            done = true;
         }
         else
	    cout << "No Match";

I wrapped this in a do...while loop. I got it to exit when I type the EXIT_CODE. My problem now is when I search for say Bob Riley it will go into an infinite loop displaying the contents of the first object which is Fred Smith in my example. Now if I type Bob Jones it will go into an infinite loop displaying the contents of the first object, again it is Fred Smith, as well as displaying No Match. What is wrong with my logic? I need it to search the entire array of objects and compare it. Help would be GREATLY appreciated.

do
   {
       bool done = false;
       int  cntr    = 0;
       cout << "Enter first and last name (case sensative) of the employee"
	      << " you want to find: \n";
       cout << "Type " << EXIT_CODE << " to exit\n";
       getline(cin,info);

      while(cntr < MAX_EMPLOYEE_SIZE && !done)
	  {
	     full_name = EmployeeInfo[cntr].get_first_name() + " " 
			    + EmployeeInfo[cntr].get_last_name();
         if(info == full_name)
         {
            EmployeeInfo[cntr].display_info(); 
	    done = true;
         }
		 else
		 {
			 cout << "No Match";
		 }
         }
   }
   while(info != EXIT_CODE);

You need to increment the "cntr" variable somewhere in your loop. You are repeatedly checking the first item of the array.

Just for completeness, you know if someone types in "fred smith" instead of "Fred Smith", you will hit the end of the list without finding the item.

Yeah I know that its case sensitive. Is there anybody that can give me a code snippet for this? I'm trying to get this thing out of the way for my dad, I didn't think it would take me this long.

mcriscolo gave you two pieces of good advice in post #11. You noticed the second, but not the first. Implement the first piece of advice and see what happens.

Oh, I noticed the first its just Ive tried using the counter everywhere and it just keeps coming up No Match. I've tried it in all these places:

do
   {
       bool done = false;
       int  cntr    = 0;
       cout << "Enter first and last name (case sensative) of the employee"
	      << " you want to find: \n";
       cout << "Type " << EXIT_CODE << " to exit\n";
       getline(cin,info);

      while(cntr < MAX_EMPLOYEE_SIZE && !done)
	  {
		 // cntr++;
	     full_name = EmployeeInfo[cntr].get_first_name() + " " 
			    + EmployeeInfo[cntr].get_last_name();
		 //cntr++;
         if(info == full_name)
         {
			//cntr++;
            EmployeeInfo[cntr].display_info(); 
	        done = true;
			//cntr++;
         }
		 else
		 {
			 //cntr++;
			 cout << "No Match";
			 //cntr++;
		 }
		 //cntr++;
      }
   }
   while(info != EXIT_CODE);

But the infinite loop is stopped. The last cntr++ seems the most appropriate.

Bring this:

else
{
cout << "No Match";
}

outside the loop and evaluate why the loop stopped. Use that information to display the result of the matching process.

if(done == true)
cout << "found" << endl;
else
cout << "No match" << endl;

That did take out the continuous printing of no match. Now when I type a name that is in the file it doesn't display the info it just prompts me for another name, also if I type a name that is not in the file it prompts me for another name. When I type the EXIT_CODE it says No Match and just exits like its suppose to.

do
   {
       bool done = false;
       int  cntr    = 0;
       cout << "Enter first and last name (case sensative) of the employee"
	      << " you want to find: \n";
       cout << "Type " << EXIT_CODE << " to exit\n";
       getline(cin,info);

      while(cntr < MAX_EMPLOYEE_SIZE && !done)
	  {
	     full_name = EmployeeInfo[cntr].get_first_name() + " " 
			    + EmployeeInfo[cntr].get_last_name();
         if(info == full_name)
         {
            EmployeeInfo[cntr].display_info(); 
	    done = true;
         }
         cntr++;
      }
   }
   while(info != EXIT_CODE);

   if(done == true)
      cout << "Found" << endl; 
   else
      cout << "No match" << endl;

why do you have this if statement outside your while loop ?

if(done == true)
      cout << "Found" << endl; 
   else
      cout << "No match" << endl;

Since you are in a do .. while loop, done will always be set to false in the beginning, and once you type EXIT_CODE, it will exit with done set to false.

But your display_info() function should work and display the information when it finds a name. Put a debug print statement before your compare condition to make sure that info and full name are the same.

Have you dumped/debugged the contents of the "full_name" variable to ensure that it looks the way you expect? Be sure there are no hidden carriage returns or other garbage that would possibly cause your comparison to fail.

I messed around with it for the last couple hours and I got it to work. I thank everyone for there great help, it is really appreciated.

Thanks,
n8thatsme

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.