diff options
| author | JordanHT-OIT <[email protected]> | 2021-11-13 08:07:57 -0800 |
|---|---|---|
| committer | JordanHT-OIT <[email protected]> | 2021-11-13 08:07:57 -0800 |
| commit | 826d2a206ed0c03a536a298e710409838d28033f (patch) | |
| tree | c03c59b31d7beb06671ab23cbee96f0eb8b68255 | |
| parent | Add online IDE url (diff) | |
| download | cst116-lab7-jordanht-oit-826d2a206ed0c03a536a298e710409838d28033f.tar.xz cst116-lab7-jordanht-oit-826d2a206ed0c03a536a298e710409838d28033f.zip | |
First code update
| -rw-r--r-- | CST116F2021-Lab7/CST116F2021-Lab7.cpp | 230 |
1 files changed, 218 insertions, 12 deletions
diff --git a/CST116F2021-Lab7/CST116F2021-Lab7.cpp b/CST116F2021-Lab7/CST116F2021-Lab7.cpp index 77b01eb..1ede4a6 100644 --- a/CST116F2021-Lab7/CST116F2021-Lab7.cpp +++ b/CST116F2021-Lab7/CST116F2021-Lab7.cpp @@ -1,20 +1,226 @@ -// CST116F2021-Lab7.cpp : This file contains the 'main' function. Program execution begins and ends there. -// +//Code by Jordan Harris-Toovy for OIT's CST116 lab 7, November 2021 #include <iostream> +#include <cstring> //Needed for string datatype +#include <iomanip> +#include <sstream> //Needed for getline +#include <limits> //Needed for numeric_limits +using namespace std; + + +//From class: Substring finding code REMOVE BEFORE SENDING THIS IN +/* +#define ARRAY_SIZE 100 int main() { - std::cout << "Hello World!\n"; + string testArray[ARRAY_SIZE]{ "Test1", "Test2", "Hello all" }; + string subSring{ "ll" }; + int i = 0, j = 0, k = 0; + + while ((subSring[i] != testArray[2][j]) && (j < testArray[2].size())) + { + j++; + } + if (subSring[i] == testArray[2][j]) + { + while ((subSring[i]) == testArray[2][j] && (i < subSring.size()) && (j < testArray[2].size())) + { + i++; + j++; + } + } + if (i == subSring.size()) + { + cout << "Substring " << subSring << " was found,"; + } + else + { + cout << "Substring " << subSring << " was not found,"; + } + return (0); +} +*/ + +#define MAX_STRINGS 100 +//Do not exceed 999; the output will become unevenly spaced + +bool getInt(int&); +int menu(int&); +void addString(int, string[MAX_STRINGS]); +void printStrings(int, int, string[MAX_STRINGS]); +int stringSearch(int, string, string[MAX_STRINGS]); + +int main(void) +{ + string strArray[MAX_STRINGS]{}, searchString{}; + int occupiedPositions = 0, menuSelect = 1, searchTarget = 0; + + while ((menuSelect >= 1) && (menuSelect <= 4)) + { + menuSelect = menu(occupiedPositions); + + switch (menuSelect) + { + case (1): + + addString(occupiedPositions++, strArray); + + break; + case (2): + + printStrings(0, occupiedPositions, strArray); + + break; + case (3): + + if (occupiedPositions > 0) + { + cout << "Enter string to search: "; + + getline(cin >> ws, searchString); + + searchTarget = stringSearch(occupiedPositions, searchString, strArray); + + if (searchTarget < 0) + { + cout << "No match found" << endl; + } + else + { + cout << "The string \"" << searchString << "\" was found in srting \"" << strArray[searchTarget] << "\" which is in position " << searchTarget + 1 << endl; + } + } + else + { + cout << "Cannot search: No stings stored" << endl; + } + + + break; + case (4): + + + + break; + } + } + + cout << strArray[0].size() << endl; + + cout << "Exiting" << endl; + + return (0); +} + +//Gets an input from the user, it not an int, returns false and sets input to 0 +bool getInt(int& innum) +{ + bool valid = true; + + cin >> innum; + + if (!cin.good()) // Check if the input was an int + { + cin.clear(); //Clear failure condition flag + cin.ignore(numeric_limits<streamsize>::max(), '\n'); //Clear all non ints until next \n + + valid = false; + innum = 0; + } + + return valid; } -// Run program: Ctrl + F5 or Debug > Start Without Debugging menu -// Debug program: F5 or Debug > Start Debugging menu +//Display a menu and return a control value +int menu(int& pos) +{ + int menuSel = 0; + bool valid_input = true; + + cout << "There are currently " << pos << " strings in the array." << endl + << "1) Add a string\n2) Print current strings\n3) Search for a substring\n4) Delete a string\n5) Exit" << endl; + valid_input = getInt(menuSel); + + while ((menuSel < 1) || (menuSel > 5) || !valid_input) //Check if the entered value is one of the options, otherwise, ask again + { + cout << "Invalid entry, enter again: "; + valid_input = getInt(menuSel); + } + + return menuSel; +} + +//Add a sting at the position sel into the array stringBase +void addString(int sel, string stringBase[MAX_STRINGS]) +{ + cout << "Enter string: "; + + getline(cin >> ws, stringBase[sel]); //Discards all whitespace chars and reads in the input buffer until \n +} + +//Prints srtings on their own lines from the stloc to edloc positions of the string array stringBase +void printStrings(int stloc, int edloc, string stringBase[MAX_STRINGS]) +{ + cout << endl; + + for (int idx = stloc; idx < edloc; idx++) + { + cout.setf(ios::left); + cout << setw(3) << idx + 1 << " - " << stringBase[idx] << endl; + } + + cout << endl; +} + +//Searches the first arrayDepth elements of stringBase for subString, returns -1 if not found, otherwise returns match index +int stringSearch(int arrayDepth, string subString, string stringBase[MAX_STRINGS]) +{ + int strCheck = 0, offset = 0; + + for (int idx = 0; idx < arrayDepth; idx++) //Iterate through arrayDepth members of srtingBase + { + offset = (stringBase[idx].size() - subString.size()); //Calculate the length difference beteen the search string and the selected array string -// Tips for Getting Started: -// 1. Use the Solution Explorer window to add/manage files -// 2. Use the Team Explorer window to connect to source control -// 3. Use the Output window to see build output and other messages -// 4. Use the Error List window to view errors -// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project -// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file + if (offset == 0) + { + for (int idz = 0; idz < subString.size(); idz++) //Compair each char if the lengths are the same + { + if (subString[idz] == stringBase[idx][idz]) + { + strCheck++; + } + } + if (strCheck == subString.size()) //if the number of matched chars is the length of the search string, a match was found + { + return(idx); + } + else + { + strCheck = 0; + } + } + else if (offset > 0) //If the lengths are different: + { + for (int idy = 0; idy <= offset; idy++) //Iterate through offset + { + for (int idz = 0; idz < subString.size(); idz++) //Compair each char, but offset the selected array string's starting point + { + if (subString[idz] == stringBase[idx][idz + idy]) + { + strCheck++; + } + } + if (strCheck == subString.size()) //if the number of matched chars is the length of the search string, a match was found + { + return(idx); + } + else + { + strCheck = 0; + } + } + } + } + return(-1); +}
\ No newline at end of file |