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