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
|
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void getnames(vector<string>&);
void displaynames(vector<string>&);
void sortnames(vector<string>& names);
void removename(string name,vector<string>& names);
void swap(int& x, int& y);
int main()
{
vector<string> names;
string name;
while (true) {//while loop to keep program running
string choice;
cout<<endl;
cout<<"Enter function you wish to use"<<endl;
cout<<"(getnames,displaynames,sortnames,findname,removename,quit): ";
cin>>choice;
if (choice=="getnames"){//if option to get input
getnames (names);
system ("PAUSE");
}
if (choice=="displaynames") {//if option to display names
displaynames (names);
system ("PAUSE");
}
if (choice=="sortnames"){//if option to sort the names by alphabetically
sortnames (names);
system ("PAUSE");
}
if (choice=="removename"){//if option to delete a name from the list
cout<<"Enter Name to Remove";
getline(cin,name);
removename (name, names);
system ("PAUSE");
}
if (choice=="quit") {break;}//if option to quit the program
}
return 0;
}
void getnames (vector<string>& names) {//input names for the vector
while (true) {
string name;
cout<<"Enter a name (quit to stop): ";
cin>>name;
if (name == "quit") break;
names.push_back(name);
}
}
void displaynames (vector<string>& names) {//displaying the names in order
int i;
for(i=0; i<names.size();i++){
cout<<names[i]<<endl;
}
cout<<i<<" names entered"<<endl;
}
void swap(int& x, int& y)//simple swap function
{
int temp;
temp=x;
x=y;
y=temp;
}
void sortnames(vector<string>& names) { //sorting the names in alphabetical order
for (int i = 1; i < names.size(); i++)
for (int j = 0; j < names.size() - i; j++)
if (names[j] > names[j+1]) swap(names[j], names[j+1]);
}
void removename (string Name, vector<string>& names) {//removing a name
for (int i=0; i!=names.size(); i++)
{
if (names[i].Names == names)
{
names.erase(names.begin()+i,names.begin()+1+i);
break;
}
}
}
|