aboutsummaryrefslogtreecommitdiff
path: root/CST116F2021-Lab7/Functions.cpp
diff options
context:
space:
mode:
authorJoseph Ten Eyck <[email protected]>2021-11-16 20:48:42 -0800
committerJoseph Ten Eyck <[email protected]>2021-11-16 20:48:42 -0800
commit24f1c58635a8bb9963e2750729d8b1f5e400fa05 (patch)
tree57e49850b2ba33e851b1f8a84b957d7d3d0614b3 /CST116F2021-Lab7/Functions.cpp
parentAdd online IDE url (diff)
downloadcst116-lab7-josephteneyck-master.tar.xz
cst116-lab7-josephteneyck-master.zip
Lab 7 homework program complete!HEADmaster
Diffstat (limited to 'CST116F2021-Lab7/Functions.cpp')
-rw-r--r--CST116F2021-Lab7/Functions.cpp153
1 files changed, 153 insertions, 0 deletions
diff --git a/CST116F2021-Lab7/Functions.cpp b/CST116F2021-Lab7/Functions.cpp
new file mode 100644
index 0000000..5e3335e
--- /dev/null
+++ b/CST116F2021-Lab7/Functions.cpp
@@ -0,0 +1,153 @@
+#include "Header.h"
+
+void displayMenu(int& menu_choice) //displays menu, takes in user menu choice
+{
+ do
+ {
+ cout << "\n\t\t==============================";
+ cout << "\n\t\t What would you like to do?";
+ cout << "\n\t\t==============================";
+
+ cout << "\n\n\t1) Add strings to array";
+ cout << "\n\t2) Print array";
+ cout << "\n\t3) Find string or substring in array";
+ cout << "\n\t4) Remove string based on location within array";
+ cout << "\n\t5) Exit";
+
+ cout << "\n\n\t\tMenu choice #: ";
+ cin >> menu_choice;
+
+ if (menu_choice < 1 || menu_choice > 5)
+ {
+ cout << "\n\nError: Invalid menu choice\n";
+ }
+ } while (menu_choice < 1 || menu_choice > 5);
+}
+
+void processMenuChoice(int menu_choice, string string_array[], int& current_place) //processes menu choice, calls relevant function
+{
+ switch (menu_choice)
+ {
+ case 1:
+ {
+ addStrings(string_array, current_place);
+ break;
+ }
+ case 2:
+ {
+ printArray(string_array, current_place);
+ break;
+ }
+ case 3:
+ {
+ findString(string_array, current_place);
+ break;
+ }
+ case 4:
+ {
+ removeString(string_array, current_place);
+ break;
+ }
+ case 5:
+ {
+ cout << "\n\t\t~ Goodbye! ~" << endl;
+ break;
+ }
+ }
+}
+
+void addStrings(string string_array[], int& current_place) //gives user ability to add strings to the array
+{
+ cout << "\n\t---------------------------------------------------------------------------";
+ cout << "\n\t Type a string to be added to the array then press enter to add another.";
+ cout << "\n\t To stop adding strings, type '0' and press enter.";
+ cout << "\n\t---------------------------------------------------------------------------\n";
+
+ cout << "\n\tEnter string: ";
+ cin >> string_array[current_place - 1]; //overwrites "0" that is later entered to stop the loop below
+
+ do //loop for entering strings
+ {
+ cout << "\n\tEnter string: ";
+ cin >> string_array[current_place];
+ current_place++;
+ } while (string_array[current_place - 1] != "0");
+}
+
+void printArray(string string_array[], int& current_place) //prints all strings in the array
+{
+ int count = 0;
+
+ cout << "\n\t---------------------------------------------------------------";
+ cout << "\n\t The current string array (shown with numbers for reference)";
+ cout << "\n\t---------------------------------------------------------------\n";
+
+ do
+ {
+ cout << "\n\t" << count + 1 << ". " << string_array[count];
+ count++;
+ } while (count < current_place - 1); // uses "less than" as to not print the "0" from the last place in the array
+}
+
+void findString(string string_array[], int& current_place) //gives user ability to search for strings or substrings within the array, displays if they are found
+{
+ string subString = "0"; //string or substring we are searching for
+ int i = 0, j = 0, k = 0; //i = letter of substring, j = word in array, k = letter in word in array
+
+ cout << "\n\t---------------------------------------------";
+ cout << "\n\t Enter a string or substring to search for";
+ cout << "\n\t---------------------------------------------\n";
+
+ cout << "\n\tString/substring: ";
+ cin >> subString;
+
+ while (i != subString.size() && j < MAX_STRINGS) //loops while the substring has not been found AND we have not gone through the entire array
+ {
+ i = 0; //resets these values to check the substring against the next word in the testarray
+ k = 0;
+
+ while ((subString[i] != string_array[j][k]) && (k < string_array[j].size())) //searches a single word for a letter from substring
+ k++;
+
+ if (subString[i] == string_array[j][k] && k < string_array[j].size()) //if a substring letter is found....
+ while ((subString[i] == string_array[j][k]) && (i < subString.size())
+ && k < string_array[j].size())
+ {
+ i++; //go to the next substring letter
+ k++; //move to the next teststring word letter
+ }
+ j++; //move to the next word within the testarray
+ }
+
+ if (i == subString.size()) //if i (the place in the substring) has gotten to the end of the substring (all letters found in succession)....
+ cout << "\n\tString/substring '" << subString << "' was found!" << endl;
+
+ else
+ cout << "\n\tString/substring '" << subString << "' was not found." << endl;
+}
+
+void removeString(string string_array[], int& current_place) //gives user ability to remove a string from the array based on string location, displays removed string
+{
+ int to_remove = 0;
+ string removed = "0";
+
+ cout << "\n\t----------------------------------------------------------------";
+ cout << "\n\t Enter the number of a string in the array to remove (1 - 150)";
+ cout << "\n\t----------------------------------------------------------------\n";
+
+ cout << "\n\tIndex number: ";
+ cin >> to_remove;
+
+ to_remove--; //accounts for user entering string number instead of index number
+
+ removed = string_array[to_remove];
+
+ cout << "\n\t\tThe removed string was: '" << removed << "'" << endl;
+
+ for (int i = to_remove; i < MAX_STRINGS - 1; i++) //compresses the array
+ {
+ string_array[i] = string_array[i + 1];
+ }
+
+ current_place--;
+} \ No newline at end of file