aboutsummaryrefslogtreecommitdiff
path: root/num4/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'num4/main.cpp')
-rw-r--r--num4/main.cpp167
1 files changed, 116 insertions, 51 deletions
diff --git a/num4/main.cpp b/num4/main.cpp
index 00b63bc..0ce7c61 100644
--- a/num4/main.cpp
+++ b/num4/main.cpp
@@ -18,6 +18,8 @@ void ClearBuffer();
void DisplayMenu(int&);
void ProcessMenuChoice (int&, int&, int&, std::string first[MAX], std::string last[MAX], std::string phone[MAX], std::string bday[MAX]);
+void FindName(int record_counter, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX]);
+void SortData(int record_counter, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX]);
void AddPerson(int, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX]);
void PrintData(int, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX]);
void FindString (string first[], string last[], string phone[], string bday[], int);
@@ -25,14 +27,14 @@ int ReadData (ifstream & inFile, std::string first[MAX], std::string last[MAX],
void OutData(ofstream & outFile, string first[], string last[], string phone[], string bday[], int record_counter );
-int main()
-{
- std::string f_name[MAX];
- std::string l_name[MAX];
- std::string phone[MAX];
- std::string bday[MAX];
+std::string f_name[MAX];
+std::string l_name[MAX];
+std::string phone[MAX];
+std::string bday[MAX];
+int main()
+{
int record_counter = 0;
int menu_choice;
int flag = 1;
@@ -56,10 +58,14 @@ int main()
}
//User interacts with program here.
+
+ SortData(record_counter, f_name, l_name, phone, bday);
while (flag != 0)
{
DisplayMenu(menu_choice);
ProcessMenuChoice(menu_choice, flag, record_counter, f_name, l_name, phone, bday);
+ SortData(record_counter, f_name, l_name, phone, bday);
+ ClearBuffer();
}
//Writes changes to the data.txt file
ofstream outFile ( "data.txt" );
@@ -79,6 +85,57 @@ int main()
}
+void SortData(int record_counter, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX])
+{
+ int i, j, k;
+ int flag = 0;
+ string temp_first;
+ string temp_last;
+ string temp_phone;
+ string temp_bday;
+
+ for (i = 0; i < record_counter - 1; i++)
+ {
+ transform(last[i].begin(), last[i].end(), last[i].begin(), ::tolower);
+ transform(first[i].begin(), first[i].end(), first[i].begin(), ::tolower);
+
+ for (j = i + 1; j < record_counter; j++)
+ {
+ transform(first[j].begin(), first[j].end(), first[j].begin(), ::tolower);
+
+ for (k = 0; k < last[i].length(); k++)
+ {
+ if (last[i][k] > last[j][k])
+ {
+ temp_last = last[j];
+ last[j] = last[i];
+ last[i] = temp_last;
+
+ temp_first = first[j];
+ first[j] = first[i];
+ first[i] = temp_first;
+
+ temp_phone = phone[j];
+ phone[j] = phone[i];
+ phone[i] = temp_phone;
+
+ temp_bday = bday[j];
+ bday[j] = bday[i];
+ bday[i] = temp_bday;
+
+ k = last[i].length();
+ }
+
+ else if (last[i][k] < last[j][k])
+ {
+ k = last[i].length();
+ }
+ }
+ }
+ }
+}
+
+
int ReadData ( ifstream & inFile, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX])
{
int counter = 0;
@@ -89,49 +146,55 @@ int ReadData ( ifstream & inFile, string first[MAX], string last[MAX], string ph
counter++;
inFile >> first[counter] >> last[counter] >> phone[counter] >> bday[counter];
}
-
return counter;
}
-/* void FindString(string first[], int limit) */
-/* { */
-/* // Check for a substring inside of one of the input strings. */
-/* // Prints whether or not the string was found. If found */
-/* // prints the index of the list where the string was found */
-/* // and what the string was that contained the substring. */
-
-/* string sub; */
-/* int i, j; */
-/* int reset = 0; */
-/* ClearBuffer(); */
-
-/* cout << "Lets look for a substring..." << endl; */
-/* cout << "Please enter a substring to look for: \n" << endl; */
-/* getline(cin, sub); */
-
-/* for (i = 0; i < limit; i++) { */
-/* if (arr[i].size() < sub.size()) */
-/* i = limit; */
-/* for (j = 0; j < arr[i].size(); j++) { */
-/* int k = 0; */
-/* if (arr[i][j] == sub[k]) { */
-/* reset = j; */
-/* while (arr[i][j] == sub[k] && k < sub.size()) { */
-/* j++; */
-/* k++; */
-/* } */
-
-/* if (k == sub.size()){ */
-/* cout << "We found it at index: " << i+1 << endl; */
-/* cout << "The string was: " << arr[i] << endl; */
-/* } */
-/* else */
-/* j = reset; */
-/* } */
-/* } */
-/* } */
-/* } */
+void FindName(int record_counter, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX])
+{
+ // Check for a substring inside of one of the input strings.
+ // Prints whether or not the string was found. If found
+ // prints the index of the list where the string was found
+ // and what the string was that contained the substring.
+
+ string search;
+ int flag = 0;
+ int left = 0;
+ int right = record_counter;
+ int mid;
+
+ cout << "Enter the last name of the person you wish to find: ";
+ getline(cin, search);
+
+ transform(search.begin(), search.end(), search.begin(), ::tolower);
+
+ while (left <= right && flag == 0)
+ {
+ mid = (left + right) / 2;
+
+ if (search == last[mid])
+ {
+ cout << "WE FOUND IT!" << endl;
+ cout << first[mid] << " " << last[mid] << " " << phone[mid] << " " << bday[mid] << endl;
+ flag = 1;
+ }
+
+ else if(search > last[mid])
+ {
+ left = mid + 1;
+ }
+
+ else
+ {
+ right = mid - 1;
+ }
+ }
+
+ if (flag == 0)
+ cout << "Man, we ain't found shit." << endl;
+ /* cout << "The name you entered doesn't exist in our database. Please check spelling and try again." << endl; */
+
+}
void PrintData(int record_counter, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX])
@@ -145,26 +208,27 @@ void PrintData(int record_counter, string first[MAX], string last[MAX], string p
while (i < record_counter)
{
cout << i+1 << ") " << first[i] << " " << last[i] << " " << phone[i] << " " << bday[i] << endl;
- i += 1;
+ i++;
}
}
void AddPerson(int record_counter, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX])
{
+ int i = record_counter - 1;
std::string temp;
cout << "Enter the first name of the person you'd like to add: " << endl;
cin >> temp;
- first[record_counter] = temp;
+ first[i] = temp;
cout << "Enter the last name of the person you'd like to add: " << endl;
cin >> temp;
- last[record_counter] = temp;
+ last[i] = temp;
cout << "Enter the phone number of the person you'd like to add: " << endl;
cin >> temp;
- phone[record_counter] = temp;
+ phone[i] = temp;
cout << "Enter the birthday of the person you'd like to add: " << endl;
cin >> temp;
- bday[record_counter] = temp;
+ bday[i] = temp;
}
/* void RmString(string arr[], int limit) */
@@ -243,12 +307,13 @@ void ProcessMenuChoice (int& menu_choice, int& flag, int& record_counter, std::
switch(menu_choice)
{
case 1:
- // FindString(FNAME, LNAME, PHONE, BDAY, limit);
+ FindName(record_counter, first, last, phone, bday);
break;
case 2:
record_counter++;
AddPerson(record_counter, first, last, phone, bday);
+ SortData(record_counter, first, last, phone, bday);
break;
case 3: