diff options
Diffstat (limited to 'CST116F2021-Lab7')
| -rw-r--r-- | CST116F2021-Lab7/CST116F2021-Lab7.cpp | 20 | ||||
| -rw-r--r-- | CST116F2021-Lab7/CST116F2021-Lab7.vcxproj | 2 | ||||
| -rw-r--r-- | CST116F2021-Lab7/CST116F2021-Lab7.vcxproj.filters | 2 | ||||
| -rw-r--r-- | CST116F2021-Lab7/Lab7-Billingsley.cpp | 181 |
4 files changed, 183 insertions, 22 deletions
diff --git a/CST116F2021-Lab7/CST116F2021-Lab7.cpp b/CST116F2021-Lab7/CST116F2021-Lab7.cpp deleted file mode 100644 index 77b01eb..0000000 --- a/CST116F2021-Lab7/CST116F2021-Lab7.cpp +++ /dev/null @@ -1,20 +0,0 @@ -// CST116F2021-Lab7.cpp : This file contains the 'main' function. Program execution begins and ends there. -// - -#include <iostream> - -int main() -{ - std::cout << "Hello World!\n"; -} - -// Run program: Ctrl + F5 or Debug > Start Without Debugging menu -// Debug program: F5 or Debug > Start Debugging menu - -// 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 diff --git a/CST116F2021-Lab7/CST116F2021-Lab7.vcxproj b/CST116F2021-Lab7/CST116F2021-Lab7.vcxproj index 5aec3b1..b1b2226 100644 --- a/CST116F2021-Lab7/CST116F2021-Lab7.vcxproj +++ b/CST116F2021-Lab7/CST116F2021-Lab7.vcxproj @@ -139,7 +139,7 @@ </Link> </ItemDefinitionGroup> <ItemGroup> - <ClCompile Include="CST116F2021-Lab7.cpp" /> + <ClCompile Include="Lab7-Billingsley.cpp" /> </ItemGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <ImportGroup Label="ExtensionTargets"> diff --git a/CST116F2021-Lab7/CST116F2021-Lab7.vcxproj.filters b/CST116F2021-Lab7/CST116F2021-Lab7.vcxproj.filters index e6b507e..4f35fec 100644 --- a/CST116F2021-Lab7/CST116F2021-Lab7.vcxproj.filters +++ b/CST116F2021-Lab7/CST116F2021-Lab7.vcxproj.filters @@ -15,7 +15,7 @@ </Filter> </ItemGroup> <ItemGroup> - <ClCompile Include="CST116F2021-Lab7.cpp"> + <ClCompile Include="Lab7-Billingsley.cpp"> <Filter>Source Files</Filter> </ClCompile> </ItemGroup> diff --git a/CST116F2021-Lab7/Lab7-Billingsley.cpp b/CST116F2021-Lab7/Lab7-Billingsley.cpp new file mode 100644 index 0000000..c571ef0 --- /dev/null +++ b/CST116F2021-Lab7/Lab7-Billingsley.cpp @@ -0,0 +1,181 @@ +// File: Lab7-Billingsley.cpp +// Summary: The completed lab 7 program. Code added in 12b and 11c are marked +// Author: Logan Billingsley +// Date: 11/16/2021 +#include <iostream> +#include <cstring> +#include <string> // for getine + +using namespace std; + +const int STRING = 100; // Max number of strings + +void enterString(string[], int&); +void printString(string[], int); +void findString(string[], int); // Added code from 12b +void findSub(string[], int); // Added code from 12b +void deleteString(string[], int&); // Added code from 11c + +int main() +{ + string list[STRING]; + int size = 0; + int menu; // Menu Choice + string repeat; // Used in looping menu + + enterString(list, size); + + do + { + cout << "Menu:\n" + << "1: Enter strings\n" + << "2: Print strings\n" + << "3: Find string\n" // Added code from 12b + << "4: Find sub-string\n" // Added code from 12b + << "5: Delete string\n" // Added code from 11c + << "6: exit\n\n"; + cin >> menu; + cin.ignore(180, '\n'); // Used here so future getline functions work + cout << endl; + switch (menu) + { + case 1: + enterString(list, size); + break; + case 2: + printString(list, size); + break; + case 3: // Added code from 12b + findString(list, size); + break; + case 4: // Added code from 12b + findSub(list, size); + break; + case 5: // Added code from 11c + deleteString(list, size); + break; + default: + cout << "Exiting program\n"; + return 0; + } + cout << "Do you wish to select another program?\n" + << "Please input \"Yes\" or \"No\": "; + cin >> repeat; + cout << endl; + } while (repeat == "yes" || repeat == "Yes" || repeat == "YES"); // While selected phrase to repeat is entered + + return 0; +} +void enterString(string list[], int& size) +{ + cout << "Please enter each string separated by a newline.\n" + << "Once you want to stop inputting strings, type \"exit\"\n\n"; + for (int i = size; i < STRING; i++) // Repeat until the limit (100 strings) + { + size = i; // Set size = i after increasing it. I would just use size in the for loop, but that caused problems. + cout << i + 1 << ":\t\b\b\b"; // I would rather do setw(4) however that is right-aligned + getline(cin, list[i], '\n'); + if (list[i] == "exit" || list[i] == "Exit" || list[i] == "EXIT") // Exit if input string is selected exit phrase + break; + } + if (size == 99) // 0-99 is the 100 strings, therefor 99 is max. + cout << "\nSize limit reached\n"; + cout << "Returning to menu\n\n"; +} +void printString(string list[], int size) +{ + cout << "Printing inputted strings:\n\n"; + + for (int i = 0; i < size; i++) // For all input strings + { + cout << i + 1 << ":\t\b\b\b" << list[i] << endl; // I would rather do setw(4) however that is right-aligned + } +} +void findString(string list[], int size) // Added code from 12b +{ + int found = 0; // Number of strings found + string search[1]; // String + cout << "Please enter string you want to search for:\n\n"; + getline(cin, search[0], '\n'); + for (int i = 0; i < size; i++) // Search each string + { + if (list[i] == search[0]) // If strings match + { + cout << "Found string " << i + 1 << endl; + found++; // Add 1 to number of strings found + } + } + if (found == 1) // Formatting so grammatically correct + cout << "Found 1 string.\n\n"; + else + cout << "Found " << found << " strings.\n\n"; +} +void findSub(string list[], int size) // Added code from 12b +{ + int found = 0; // Number of cases found + string search[1]; // Substring + cout << "Please enter substring you want to search for:\n\n"; + getline(cin, search[0], '\n'); // Get substring + for (int i = 0; i < size; i++) // Search each string + { + int k = 0; // Set the current substring to 0; + int n = 0; // Set number of substrings found per string to 0 + for (int j = 0; j < list[i].size(); j++) // Search each character in the current string + { + if (list[i][j] == search[0][k]) // If current string character is equal to the current substring character + { + k++; // Add to characted matched + if (k == search[0].size()) // If character all characters match + { + found++; // Add to number of cases found + k = 0; // Reset current substring character to search more in that string + n++; // Add 1 to the number of substrings found per string + } + } + + } + if (n > 0) // If found substring in string + { + if (n == 1) // Formatting so grammatically correct + cout << "Found 1 instance of the substring: \"" << search[0] << "\" in the string: \"" << list[i] << "\"\n\n"; + else + cout << "Found " << n << " instances of the substring: \"" << search[0] << "\" in the string: \"" << list[i] << "\"\n\n"; + } + } + if (found == 1) // Formatting so gramatically correct + cout << "Found 1 instance of the substring: \"" << search[0] << "\"\n\n"; + else + cout << "Found " << found << " instances of the substring: \"" << search[0] << "\"\n\n"; +} +void deleteString(string list[], int& size) // Added code from 11c +{ + int del; // Which string to delete 1-100 + bool pass = 0; // If check is passed + cout << "Select which string you want to delete\n" + << "Please enter the number of that string. If none, enter \"0\": "; + do + { + cin >> del; + cout << endl; + if (del >= 0 && del <= size) // If string selected is within range + pass = 1; + else + { + cout << "Invalid number.\n" + << "Please try again: "; + } + } while (pass == 0); // If check is passed + if (del == 0) // Exit if input string is selected exit phrase + { + cout << "Returning to Menu\n\n"; + return; + } + del--; // Convert to counting from 0 + cout << "Deleting string number " << del + 1 << ": " << list[del] << endl << endl; + for (int i = del; i < size; i++) + { + list[i] = list[i + 1]; // Make each string equal the one after it (effectively erasing it) + } + size--; // Remove the empty string from the end of the array + cout << "Returning to Menu\n\n"; +}
\ No newline at end of file |