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
|
#include <iostream>
#include <string>
using namespace std;
int getEntries (string user[], string phone[])
{
int i=0;
int w=0;
do{
cout << "Enter name: ";
getline (cin, user[i]);
if(user[i] == "***END***")
break;
cout << "Enter number: ";
getline (cin, phone[i]);
i++;
}while ((i<10) && (user[i] != "***END***"));
return i;
}
int addEntries(string user[], string phone[])
{
int i = 0;
string totaluser,totalphone;
cout << "Enter the name: ";
cin.ignore();
getline (cin, user[i]); //<-- i = 0
cout << "Enter number: ";
getline (cin, phone[i]); //<-- i = 0
i++;
return i;
}
void sortBook (string names[],int entries, string phone[])
{
int first;
string temp;
string temp2;
for(int i=entries-1; i>0; i--)
{
first = 0;
for (int j=1; j<=i; j++)
{
if (names[j] > names[first])
{
first = j;
}
temp = names[first];
names[first] = names[i];
names[i] = temp;
temp2 = phone[first];
phone[first] = phone[i];
phone[i] = temp2;
}
}
}
void display ( string names[], int entries, string phone[])
{
for (int i=0; i<entries; i++)
cout << names[i] << " " << phone[i] << endl;
}
void searchArray (string names[], string phone[], int entries)
{
string key;
cout << "Who's number do you want?";
cin >> key;
for(int i=0; i<entries; i++)
{
if(names[i] == key)
cout << "The number is " << phone[i] << endl;
}
}
int main ()
{
int i;
string user[10];
string phone[10];
string choice;
i = getEntries (user, phone);
while (choice != "quit")
{
cout << "Enter menu choice (display, add, search, quit): ";
cin >> choice;
if(choice == "a")
{
addEntries(user,phone);
}
if(choice == "d")
{
sortBook (user,i, phone);
display (user,i, phone);
}
if(choice == "s")
{
searchArray(user,phone,i);
}
}
return 0;
}
|