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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
using namespace std;
struct Person
{
string firstName;
string lastName;
int age;
};
void getNames(vector <Person> &people);
void display(const vector <Person> &people);
void open(vector <Person> &people);
void save(ofstream &fout, vector <Person> people);
void remove(ofstream &fout, vector <Person> &people);
void sort(vector <Person> &people);
int main()
{
ofstream fout;
vector <Person> people;
cout << "Welcome to the Names Catalogue. Please start by opening the file." << endl;
cout << endl;
int sel;
while (true)
{
cout << "--------------------------" << endl;
cout << "What would you like to do?" << endl;
cout << "--------------------------" << endl;
cout << "Enter 1 add entries." << endl;
cout << "Enter 2 to display entries." << endl;
cout << "Enter 3 to save entries." << endl;
cout << "Enter 4 to open file." << endl;
cout << "Enter 5 to remove an entry." << endl;
cout << "Enter 6 to sort by last namme." << endl;
cout << "Enter 7 to quit." << endl;
cout << "--------------------------" << endl;
cout << "Enter selection: ";
cin >> sel;
cout << "--------------------------" << endl;
switch (sel)
{
case 1: getNames(people); break;
case 2: display(people); break;
case 3: save(fout, people); break;
case 4: open(people); break;
case 5: remove(fout, people); break;
case 6: sort(people); break;
case 7: cout << "Goodbye."; break;
default: cout << "Error: Incorrect input. Please try again." << endl; break;
}
if (sel == 7) break;
}
fout.close();
}
void getNames(vector <Person> &people)
{
Person aPerson;
while (aPerson.firstName != "quit")
{
cout << "Enter first name, last name, and age: ";
cin >> aPerson.firstName;
if(aPerson.firstName=="quit") break;
cin >> aPerson.lastName >> aPerson.age;
people.push_back(aPerson);
}
}
void display(const vector <Person> &people)
{
for (int i = 0; i < people.size(); i++)
{
cout << "Name: " << people[i].lastName << ", " << people[i].firstName << ". Age: " << people[i].age << endl;
}
cout << endl;
}
void open(vector <Person> &people)
{
ifstream fin("names.txt");
Person aPerson;
string tmp;
stringstream ss;
if (fin.fail())
{
cout << "No file to open." << endl;
}
else cout << "File opened." << endl;
while(!fin.eof())
{
getline(fin, aPerson.lastName,';');
getline(fin, aPerson.firstName,';');
getline(fin, tmp);
ss << tmp;
ss >> aPerson.age;
people.push_back(aPerson);
}
cout << endl;
}
void save(ofstream &fout, vector <Person> people)
{
fout.open("names.txt");
for(int i=0; i < people.size(); i++)
{
fout << people[i].lastName << ';';
fout << people[i].firstName << ';';
fout << people[i].age;
if(i < people.size() -1)
fout << endl;
}
cout << "Entries saved successfully." << endl;
cout << endl;
}
void remove(ofstream &fout, vector <Person> &people)
{
int loc=people.size();
string remove;
cout << "Enter the last name of who you want to remove: ";
cin >> remove;
for (int i=0; i < people.size(); i++)
{
if (people.at(i).lastName == remove)
{
loc = i;
}
}
if(loc!=people.size())
{
for (int j = loc; j < people.size()-1; j++)
{
swap(people[j], people[j+1]);
}
people.pop_back();
cout << "Entry Removed." << endl;
}
else cout << "Name not found." << endl;
fout.open("names.txt");
for(int i=0; i < people.size(); i++)
{
fout << people[i].lastName << ';';
fout << people[i].firstName << ';';
fout << people[i].age;
if(i < people.size() -1)
fout << endl;
}
cout << endl;
}
void sort(vector <Person> &people)
{
int n = people.size();
for (int i=1; i < n; i++)
for (int j=0; j < n-1; j++)
{
if (people[j].lastName > people[j+1].lastName)
{
swap (people[j], people[j+1]);
}
}
cout << "Entries sorted successfully." << endl;
cout << endl;
}
|