Hi everyone!
I'm pretty new to c++ (Like yesterday) and I downloaded Code::Blocks and started to get to work on learning it. I ran into a problem though for some reason this code below will say it found the person even though I did not even put the correct fullname to search for.
#include <iostream>
usingnamespace std;
struct database
{
int age;
string firstName;
string lastName;
string skill;
string fullName = firstName + lastName;
};
database employees[3];
bool doesEmployeeExist(string fullName);
database getEmployee(string fullName);
int main()
{
database data;
data.firstName = "bob";
data.lastName = "builder";
data.age = 55;
data.skill = "Programming";
employees[1] = data;
cout << "Please enter the employee your looking for:" << endl;
string lookingForFullName;
cin>>lookingForFullName;
cin.ignore();
if (!doesEmployeeExist(lookingForFullName))
{
cout << "Could not find that employee!" << endl;
cin.get();
return 0;
}
cout << "We found the person you were looking for!" << endl;
cin.get();
return 1;
}
bool doesEmployeeExist(string fullName)
{
for (int a=0;a<3;a++)
{
database testData = employees[a];
if (testData.fullName == fullName)
{
returntrue;
}
}
returnfalse;
}
database getEmployee(string fullName)
{
database returningProfile;
for (int a=0; a<3; a++)
{
database maybeEmployee = employees[a];
if (maybeEmployee.fullName == fullName)
{
returningProfile = maybeEmployee;
}
}
return returningProfile;
}
Two other questions I had number one being is there a better way to check if the person your looking for is in an []?
Secondly on the input of the console were it assigns the lookingForFullName if I was to put a space in it only takes in the first arg. Any way to make that take in all args following the first?
Thank you for reading and I hope you can help!
is there a better way to check if the person your looking for is in an []
If the array is sorted, yea.
Secondly on the input of the console were it assigns the lookingForFullName if I was to put a space in it only takes in the first arg. Any way to make that take in all args following the first?
I ran into a problem though for some reason this code below will say it found the person even though I did not even put the correct fullname to search for.
If you had said "It won't find the person, no matter what fullname I input," I would've believed that. As it is, the behavior you describe can't happen as the only value any database in the employees array has for the member fullname is an empty string.