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
|
#include <iostream>
#include <string>
using namespace std;
//Prototypes-------------------------------------------------------------------------------------------------
void addName (string nameList[], const int listMax); //Adds a name
void removeName (string nameList[], const int listMax); //Removes a name
void findName (string nameList[], const int listMax); // Searches for a name in the saved list
void showList (string nameList[], const int listMax); //Shows a list of names currentllistMax stored
void sortList (string nameList[], const int listMax); //Sorts saved names in alphabetical order
//Global Declartions-----------------------------------------------------------------------------------------
const int listMax = 5;
// MAIN FUNCTION -----------------------------------------------------------------------------------------
int main(){
char input; // Variable for when the user inputs their menu selection
string nameList[listMax]; //string called names with the size being namesMax which is 50
while(true){
cout << "Enter a Command " << endl; //Prompt to choose command from menu
cout << "<A> - Add a name to the database(Max 5)." << endl; //getNames
cout << "<R> - Remove a name from the database" << endl; //removeName
cout << "<F> - Search for a name in the database" << endl; //findName
cout << "<L> - Shows current state of list" << endl; //displalistMaxNames
cout << "<S> - Sorts the database." << endl; //sortNames
cout << "<Q> - Ends the program." << endl; //Exits the program entirellistMax
cin >> input; //Saves the menu choice input
if (input == 'A' ||input == 'a')
{
addName(nameList, listMax);
}
else if (input == 'R' || input == 'r')
{
removeName(nameList, listMax);
}
else if (input == 'F' || input == 'f')
{
findName(nameList, listMax);
}
else if (input == 'L' || input == 'l')
{
showList(nameList, listMax);
}
else if (input == 'S' || input == 's')
{
sortList(nameList, listMax);
}
else if (input == 'Q' || input == 'q')
{
return 0;
}
}
}
// ADD NAMES FUNCTION ---------------------------------------------------------------------
void addName(string nameList[], const int listMax){ //WORKS
int x;
for (x = 0; x < listMax; x++)
{
cout << "Enter a name to be added to the database: ";
cin >> nameList[x];
}
}
// REMOVE NAMES FUNCTION ------------------------------------------------------------------
void removeName(string nameList[], const int listMax){ //DOESNT WORK
string clearName;
string removeName;
cout << "Enter the name to be removed from the database: ";
cin >> removeName;
for (int x = 0; x < listMax; x++)
{
if(nameList[x] == removeName)
{
nameList[x] = clearName;
}
else if(x == listMax - 1)
{
cout << "Name not found";
}
}
return;
}
// SEARCH FUNCTION ------------------------------------------------------------------------
void findName(string nameList[], const int listMax){ //DOESNT WORK
int x = 0;
int z = 0;
bool f = false;
string searchName;
cout << "Enter a name to search for: ";
cin >> searchName;
while (!f && x <= listMax-1)
{
int listMax = (x + z) / 2;
if (nameList[listMax-1] == searchName)
{
f = true;
cout << nameList[listMax-1] << "is on the list!" <<endl;
}
else if (nameList[listMax-1] > searchName)
z = listMax - 1;
else
x = listMax + 1;
}
return;
}
// DISPLAY FUNCTION ---------------------------------------------------------
void showList(string nameList[], const int listMax){ //WORKS
for(int i=0;i<listMax;i++)
cout<<nameList[i]<<endl;
return;
}
// SORT NAMES FUNCTION -----------------------------------------------------
void sortList(string nameList[], const int listMax){ //WORKS
bool sort;
do
{
sort = 0;
for (int x = 0; x < listMax - 1; x++)
{
if (nameList[x] > nameList[x + 1])
{
nameList[x].swap(nameList[x+1]);
sort = 1;
}
}
}
while (sort == 1);
return;
}
|