Text File Searching Function not Working

As a lab, we were supposed to create a program to search through a text file. We were given the following text file:


Hillary Clinton F 1947 10 26
Donald Trump M 1946 06 14
Bernie Sanders M 1941 09 08
Barack Obama M 1961 08 04
Melania Trump F 1970 04 26
Michelle Obama F 1964 01 17


When I use the search function and search for 'M', this is what I get:

Would you like to: 
1. Search for person 
2. Display information 
3. Exit 
1
Enter the name, gender, or date to search for: 
M
5 result(s) of M found. 

Result #1: 
Donald  Trump   M 1946 06 14

Result #2: 
Bernie Sanders  M 1941 09 08

Result #3: 
Barack Obama    M 1961 08 04

Result #4: 
Melania Trump   F 1970 04 26

Result #5: 
Michelle Obama  F 1964 01 17

Would you like to: 
1. Search for person 
2. Display information 
3. Exit 


Last I checked, Melania Trump and Michelle Obama were females. Oddly though, the search function works with 'F'

Would you like to: 
1. Search for person 
2. Display information 
3. Exit 
1
Enter the name, gender, or date to search for: 
F
3 result(s) of F found. 

Result #1: 
Hillary Clinton F 1947 10 26

Result #2: 
Melania Trump   F 1970 04 26

Result #3: 
Michelle Obama  F 1964 01 17

Would you like to: 
1. Search for person 
2. Display information 
3. Exit 



Here's my code:

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;

const int people = 30;

class Person
{
    public:
        Person();
        Person (string fname, string lname, string s, int byear, int bmth, 
                int bday);
        void findName(string lname, int count) const;
        void setName(string fname, string lname);
    
    private:
        string firstName, lastName, sex;
        int birthYear, birthMonth, birthDay;
};

Person::Person()
{
    firstName = "no first name";
    lastName = "no last name";
    sex = "no gender";
    birthDay = 0;
    birthYear = 0;
    birthMonth = 0;
}

Person::Person (string fname, string lname, string s, int byear, int bmth, int bday)
{
    firstName = fname;
    lastName = lname;
    sex = s;
    birthYear = byear;
    birthMonth = bmth;
    birthDay = bday;
}

string adjust_case (string name)
{
    bool in_word = false;
    
    for (char & c : name)
    {
        if (isalpha(c))
        {
            if (in_word)
                c = tolower(c);
            
            else
            {
                c = toupper(c);
                in_word = true;
            }
        }
        
        else in_word = false;
    }
    
    return name;
}

void Person::findName (string search, int count) const
{
    string line, lineArray[people];
    ifstream inFile;
    ofstream outStream;
    bool found = false;
    int results = 0;
    
    inFile.open("peopleInfo.txt");
    
    size_t pos;
    while(inFile.good())
    {
        getline (inFile,line);
        pos = line.find (search);
        
        if (pos != string::npos)
        {
            lineArray[results] = line;
            results++;
            found = true;
        }
    }
    
    inFile.close();
    
    if (!found)
        cout << search << " not found. " << endl;
    
    else if (found)
    {
        cout << results << " result(s) of " <<  search << " found. " << endl;
        
        for (int j = 0; j < results; j++)
        {
            cout << "\nResult #" << j + 1 << ": " << endl;
            cout << lineArray [j] << endl;
        }
        cout << endl;
    }
}

int main()
{
    string search, line;
    int count = 0, input;
    vector<Person> person(people);
    ifstream inStream;
    
    do
    {
        cout << "Would you like to: " << endl;
        cout << "1. Search for person "
             << "\n2. Display information "
             << "\n3. Exit " << endl;
        cin >> input;
        
        while ((input != 1 && input != 2 && input != 3) || cin.fail())
        {
            cin.clear();
            cin.ignore(1000, '\n');
            cout << "Invalid choice. Try again: " << endl;
            cout << "Would you like to: " << endl;
            cout << "1. Search for person "
                 << "\n2. Display information "
                 << "\n3. Exit " << endl;
            cin >> input;
        }
        
        if (input == 1)
        {
            cout << "Enter the name, gender, or date to search for: " << endl;
            cin.ignore();
            getline(cin, search);
            adjust_case (search);
            
            person[count].findName(search, count);
            count++;
        }
        
        else if (input == 2)
            while (getline (inStream,line))
                cout << line << '\n';
        
        else if (input == 3)
            break;
    } while (count <= people);
}


Can anybody tell me what I'm doing wrong and how to fix this?

Thanks,
VX
Last I checked, Melania Trump and Michelle Obama were females. Oddly though, the search function works with 'F'

Yes but both of these names have an "M'. Remember that getline() gets an entire line. You need to parse this "line" to separate the first name, last name, gender, birth year, birth day, birth month to place them into your class variables.

Also you probably should have a vector/array of your class and then read the entire file once placing the information into this vector, then search the vector not the file.

When you search for M it will find the first letter of their first name.
Melania Trump F 1970 04 26
Michelle Obama F 1964 01 17
Not sure what exactly is demanded but it would be much easier if you read the file into the vector of Person and search the vector.
Oooohhhh, that was so stupid of me. :P Thank you all. Will post updated code later.

Thanks,
VX
Topic archived. No new replies allowed.