Isn't working for some reason...

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.
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
#include <iostream>

using namespace 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)
        {
            return true;
        }
    }
    return false;
}

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!
Try comparing using :http://www.cplusplus.com/reference/string/string/compare/

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?

Use std::getline()
http://www.cplusplus.com/reference/string/string/getline/
Still isn't working even with the new compare. I still can not figure out why.
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
#include <iostream>

using namespace 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.compare(fullName))
        {
            return true;
        }
    }
    return false;
}

database getEmployee(string fullName)
{
    database returningProfile;
    for (int a=0; a<3; a++)
    {
        database maybeEmployee = employees[a];
        if (maybeEmployee.fullName.compare(fullName))
        {
            returningProfile = maybeEmployee;
        }
    }
    return returningProfile;
}
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.

You should have an #include <string>, as well.

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
#include <iostream>
#include <string>

using namespace std;
struct database
{
    int age;
    string firstName;
    string lastName;
    string skill;

    string fullName() const { return firstName + ' ' + lastName; }  // ***
};
database employees[3];

bool doesEmployeeExist(string fullName);
database getEmployee(string fullName);

int main()
{
    database data = { 55, "bob", "builder", "Programming" } ;
    employees[0] = data ;

    cout << "Please enter the employee your looking for:" << endl;

    string lookingForFullName;
    std::getline(std::cin, lookingForFullName) ;          // ***

    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)             // ***
        {
            return true;
        }
    }
    return false;
}

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


Changes marked with // ***
Topic archived. No new replies allowed.