diff options
| author | JordanHT-OIT <[email protected]> | 2021-11-13 08:44:20 -0800 |
|---|---|---|
| committer | JordanHT-OIT <[email protected]> | 2021-11-13 08:44:20 -0800 |
| commit | a77e9233147e9f7705a34b3530cdabdf2418534b (patch) | |
| tree | 1a134a059d6e1cba5598703eb0c81ec11ed09cca | |
| parent | First code update (diff) | |
| download | cst116-lab7-jordanht-oit-a77e9233147e9f7705a34b3530cdabdf2418534b.tar.xz cst116-lab7-jordanht-oit-a77e9233147e9f7705a34b3530cdabdf2418534b.zip | |
Second code update
| -rw-r--r-- | CST116F2021-Lab7/CST116F2021-Lab7.cpp | 55 |
1 files changed, 48 insertions, 7 deletions
diff --git a/CST116F2021-Lab7/CST116F2021-Lab7.cpp b/CST116F2021-Lab7/CST116F2021-Lab7.cpp index 1ede4a6..72024a0 100644 --- a/CST116F2021-Lab7/CST116F2021-Lab7.cpp +++ b/CST116F2021-Lab7/CST116F2021-Lab7.cpp @@ -50,6 +50,7 @@ int menu(int&); void addString(int, string[MAX_STRINGS]); void printStrings(int, int, string[MAX_STRINGS]); int stringSearch(int, string, string[MAX_STRINGS]); +int removeString(int, string[MAX_STRINGS]); int main(void) { @@ -100,15 +101,13 @@ int main(void) break; case (4): - + occupiedPositions = removeString(occupiedPositions, strArray); break; } } - cout << strArray[0].size() << endl; - - cout << "Exiting" << endl; + cout << endl << "Exiting" << endl; return (0); } @@ -164,10 +163,17 @@ void printStrings(int stloc, int edloc, string stringBase[MAX_STRINGS]) { cout << endl; - for (int idx = stloc; idx < edloc; idx++) + if (stloc == edloc) //Leave if stringBase is empty { - cout.setf(ios::left); - cout << setw(3) << idx + 1 << " - " << stringBase[idx] << endl; + cout << "No strings to display"; + } + else + { + for (int idx = stloc; idx < edloc; idx++) + { + cout.setf(ios::left); + cout << setw(3) << idx + 1 << " - " << stringBase[idx] << endl; + } } cout << endl; @@ -223,4 +229,39 @@ int stringSearch(int arrayDepth, string subString, string stringBase[MAX_STRINGS } } return(-1); +} + +//Removes the string at a user-promted location from stringBase, returns the new length of stringBase +int removeString(int arrayDepth, string stringBase[MAX_STRINGS]) +{ + int removeLoc = -1; + bool valid_input = true; + + if (arrayDepth <= 0) + { + cout << "No strings to remove" << endl; + + return (arrayDepth); + } + + cout << "Select a string to remove: "; + valid_input = getInt(removeLoc); + + while ((removeLoc < 1) || (removeLoc > arrayDepth) || !valid_input) //Check if the entered index is within the array, otherwise, ask again + { + cout << "Invalid entry, enter again: "; + valid_input = getInt(removeLoc); + } + + if (removeLoc == arrayDepth) //Just return arrayDepth if user input is the last string + { + return (arrayDepth - 1); + } + + for (int idx = --removeLoc; idx < arrayDepth; idx++) + { + stringBase[idx] = stringBase[idx + 1]; + } + + return (arrayDepth - 1); }
\ No newline at end of file |