CST116 Module 7: Lab 7 12a Write a program that includes a function to read (at least 100) strings into an array. Allow the user to either add a string or print out the contents of the array. Be sure to stop “printing” at the end of the entered values -- don’t “print” after the end of the user’s entered data. Be sure to give the user the chance to leave the program. 10 pts Submit: code & runs CODE: // Tyler Taormina // CST 116 // Lab 7 // Number 1 #include #include #include using namespace std; void GatherStrings(); void getString(); void ClearBuffer(); #define MAX 100 int main() { // program driver GatherStrings(); return 0; } void GatherStrings() { // Ask user for input of a string. Let user decide after each // input whether or not they will add another string. Once // they choose to not input, all previously entered strings // will be printed. string usr_data; string array[MAX]; int i = 0; int limit = 0; int flag = 1; while (flag && limit < MAX) { cout << setw(10) << left << "Please Enter a string to store in our database: "; getline (cin, usr_data); array[limit] = usr_data; limit += 1; cout << "\n\nEnter [1] to add another string to the data base." << endl; cout << "OR" << endl; cout << "Enter [0] to stop and see all the entries we have so far: "; cin >> flag; ClearBuffer(); } cout << "==================================================================\n"; cout << "This is what we have in our database so far..." << endl; cout << "==================================================================\n"; while (i < limit) { cout << array[i] << endl; i += 1; } } void ClearBuffer() { // clears buffer after menu choice so as not to interfere with the following user inputs. char c; do { c = getchar(); } while (c != '\n' && c != EOF); } RUN: Please Enter a string to store in our database: string 1 Enter [1] to add another string to the data base. OR Enter [0] to stop and see all the entries we have so far: 1 Please Enter a string to store in our database: string 2 Enter [1] to add another string to the data base. OR Enter [0] to stop and see all the entries we have so far: 1 Please Enter a string to store in our database: string 3 Enter [1] to add another string to the data base. OR Enter [0] to stop and see all the entries we have so far: 1 Please Enter a string to store in our database: !@#$%^& Enter [1] to add another string to the data base. OR Enter [0] to stop and see all the entries we have so far: 1 Please Enter a string to store in our database: Coffee and clean code Enter [1] to add another string to the data base. OR Enter [0] to stop and see all the entries we have so far: 0 ================================================================== This is what we have in our database so far... ================================================================== string 1 string 2 string 3 !@#$%^& Coffee and clean code =============================================================================== 12b Add to your program from part 12a: An option in a function that can find a string or substring in a string in the array. This need just be the first occurrence of the string or substring. 10 pts Submit: code & runs CODE: // tyler taormina // cst 116 // lab 7 // number 1 #include #include #include using namespace std; void RmString(string arr[], int); void GatherStrings(int& limit, string arr[]); void ClearBuffer(); void DisplayMenu(int&); void ProcessMenuChoice(int, int, string arr[]); void PrintString(int, string arr[]); void FindString(string arr[], int); #define max 100 int main() { // program driver int i = 0; int limit = 0; int count = 0; int menu_choice; string data[max]; GatherStrings(limit, data); DisplayMenu(menu_choice); ProcessMenuChoice(menu_choice, limit, data); return 0; } void GatherStrings(int& limit, string arr[]) { // ask user for input of a string. let user decide after each // input whether or not they will add another string. once // they choose to not input, all previously entered strings // will be printed. cout << "==================================================================\n"; cout << " LETS BUILD A DATABASE OF STRINGS" << endl; cout << "==================================================================\n"; string usr_data; int i = 0; int flag = 1; while (flag && limit < max) { cout << "Please Enter a string to store in our database: \n" << endl; getline (cin, usr_data); arr[limit] = usr_data; limit += 1; cout << "\n\nEnter [1] to add another string to the data base." << endl; cout << "OR" << endl; cout << "Enter [0] to STOP: \n" << endl; cin >> flag; ClearBuffer(); } } void FindString(string arr[], 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, k; 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 PrintString(int limit, string arr[]) { //Displays all strings stored. int i = 0; cout << "==================================================================\n"; cout << "This is what we have in our database so far..." << endl; cout << "==================================================================\n"; while (i < limit) { cout << i+1 << ") " << *(arr+i) << endl; i += 1; } } void RmString(string arr[], int limit) { // Finds string within array and removes the element. // May need to use vector. int i = 0, j = 0, k = 0; int choice; string deleted; string copy[max]; cout << "What data entry would you like to delete..." << endl; PrintString(limit, arr); cout << "Please enter the number for the string that you'd like to delte: " << endl; cin >> choice; choice -= 1; cout << "Working...\n\n" << endl; for (j = 0 ; j < limit + 1; j++) { if (k == choice) { cout << "Deleting...\n\n" << endl; deleted = arr[k]; k++; j--; } else { copy[j] = arr[k]; k++; } } cout << "==================================================================\n"; cout << "Here is our updated database..." << endl; cout << "==================================================================\n"; while (i < (limit - 1)) { cout << i+1 << ") " << copy[i] << endl; i++; } cout << "We removed: " << deleted << endl; } void DisplayMenu(int& menu_choice) { //Displays the menu of functions for the user to choose from. bool flag = 0; cout << "==================================================================\n"; cout << " MENU" << endl; cout << "==================================================================\n"; cout << "1) Enter 1 to search for a substring.\n"; cout << "2) Enter 2 to remove a string from the database.\n"; cout << "3) Enter 3 to print the database.\n"; cout << "4) Exit Program.\n\n"; cout << "Enter: "; cin >> menu_choice; if (menu_choice > 4 || menu_choice < 1) { cout << "Invalid Entry. Please enter a number from the options list provided.\n\n\n\n" << endl; DisplayMenu(menu_choice); } } void ProcessMenuChoice (int menu_choice, int limit, string usr_data[]) { //Takes in user input for menu choice and calls the appropriate function. int program_rerun = 0; bool check; switch(menu_choice) { case 1: FindString(usr_data, limit); break; case 2: RmString(usr_data, limit); break; case 3: PrintString(limit, usr_data); break; case 4: cout << "End program." << endl; break; default: break; } cout << "Press 1 and enter to build a new database.\n" << endl; cout << "Press any other number to exit the program.\n" << endl; cin >> program_rerun; if (program_rerun == 1) { ClearBuffer(); main(); } else { cout << "================================================================" << endl; cout << " Program Closing..." << endl; cout << "================================================================\n\n\n" << endl; } } void ClearBuffer() { // clears buffer after menu choice so as not to interfere with the following user inputs. char c; do { c = getchar(); } while (c != '\n' && c != EOF); } RUN: ================================================================== LETS BUILD A DATABASE OF STRINGS ================================================================== Please Enter a string to store in our database: tyler Enter [1] to add another string to the data base. OR Enter [0] to STOP: 1 Please Enter a string to store in our database: taormina was here Enter [1] to add another string to the data base. OR Enter [0] to STOP: 1 Please Enter a string to store in our database: hello world Enter [1] to add another string to the data base. OR Enter [0] to STOP: 1 Please Enter a string to store in our database: computer programming can tough Enter [1] to add another string to the data base. OR Enter [0] to STOP: 1 Please Enter a string to store in our database: but it can also be really fun! Enter [1] to add another string to the data base. OR Enter [0] to STOP: 1 Please Enter a string to store in our database: just keep practicing and asking questions Enter [1] to add another string to the data base. OR Enter [0] to STOP: 0 ================================================================== MENU ================================================================== 1) Enter 1 to search for a substring. 2) Enter 2 to remove a string from the database. 3) Enter 3 to print the database. 4) Exit Program. Enter: 1 Lets look for a substring... Please enter a substring to look for: hello We found it at index: 3 The string was: hello world Press 1 and enter to build a new database. Press any other number to exit the program. ============================================================================== 11c Add to your program from part 12b: An option in a function that can remove a string from the array (and compress the array) based on its location in the array. Make sure that the “print” option reflects the removed string. 10 pts Submit: code & runs Total: 30 pts CODE: // tyler taormina // cst 116 // lab 7 // number 1 #include #include #include using namespace std; void RmString(string arr[], int); void GatherStrings(int& limit, string arr[]); void ClearBuffer(); void DisplayMenu(int&); void ProcessMenuChoice(int, int, string arr[]); void PrintString(int, string arr[]); void FindString(string arr[], int); #define max 100 int main() { // program driver int i = 0; int limit = 0; int count = 0; int menu_choice; string data[max]; GatherStrings(limit, data); DisplayMenu(menu_choice); ProcessMenuChoice(menu_choice, limit, data); return 0; } void GatherStrings(int& limit, string arr[]) { // ask user for input of a string. let user decide after each // input whether or not they will add another string. once // they choose to not input, all previously entered strings // will be printed. cout << "==================================================================\n"; cout << " LETS BUILD A DATABASE OF STRINGS" << endl; cout << "==================================================================\n"; string usr_data; int i = 0; int flag = 1; while (flag && limit < max) { cout << "Please Enter a string to store in our database: \n" << endl; getline (cin, usr_data); arr[limit] = usr_data; limit += 1; cout << "\n\nEnter [1] to add another string to the data base." << endl; cout << "OR" << endl; cout << "Enter [0] to STOP: \n" << endl; cin >> flag; ClearBuffer(); } } void FindString(string arr[], 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, k; 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 PrintString(int limit, string arr[]) { //Displays all strings stored. int i = 0; cout << "==================================================================\n"; cout << "This is what we have in our database so far..." << endl; cout << "==================================================================\n"; while (i < limit) { cout << i+1 << ") " << *(arr+i) << endl; i += 1; } } void RmString(string arr[], int limit) { // Finds string within array and removes the element. // May need to use vector. int i = 0, j = 0, k = 0; int choice; string deleted; string copy[max]; cout << "What data entry would you like to delete..." << endl; PrintString(limit, arr); cout << "Please enter the number for the string that you'd like to delte: " << endl; cin >> choice; choice -= 1; cout << "Working...\n\n" << endl; for (j = 0 ; j < limit + 1; j++) { if (k == choice) { cout << "Deleting...\n\n" << endl; deleted = arr[k]; k++; j--; } else { copy[j] = arr[k]; k++; } } cout << "==================================================================\n"; cout << "Here is our updated database..." << endl; cout << "==================================================================\n"; while (i < (limit - 1)) { cout << i+1 << ") " << copy[i] << endl; i++; } cout << "We removed: " << deleted << endl; } void DisplayMenu(int& menu_choice) { //Displays the menu of functions for the user to choose from. bool flag = 0; cout << "==================================================================\n"; cout << " MENU" << endl; cout << "==================================================================\n"; cout << "1) Enter 1 to search for a substring.\n"; cout << "2) Enter 2 to remove a string from the database.\n"; cout << "3) Enter 3 to print the database.\n"; cout << "4) Exit Program.\n\n"; cout << "Enter: "; cin >> menu_choice; if (menu_choice > 4 || menu_choice < 1) { cout << "Invalid Entry. Please enter a number from the options list provided.\n\n\n\n" << endl; DisplayMenu(menu_choice); } } void ProcessMenuChoice (int menu_choice, int limit, string usr_data[]) { //Takes in user input for menu choice and calls the appropriate function. int program_rerun = 0; bool check; switch(menu_choice) { case 1: FindString(usr_data, limit); break; case 2: RmString(usr_data, limit); break; case 3: PrintString(limit, usr_data); break; case 4: cout << "End program." << endl; break; default: break; } cout << "Press 1 and enter to build a new database.\n" << endl; cout << "Press any other number to exit the program.\n" << endl; cin >> program_rerun; if (program_rerun == 1) { ClearBuffer(); main(); } else { cout << "================================================================" << endl; cout << " Program Closing..." << endl; cout << "================================================================\n\n\n" << endl; } } void ClearBuffer() { // clears buffer after menu choice so as not to interfere with the following user inputs. char c; do { c = getchar(); } while (c != '\n' && c != EOF); } RUN: ================================================================== LETS BUILD A DATABASE OF STRINGS ================================================================== Please Enter a string to store in our database: By tyler Enter [1] to add another string to the data base. OR Enter [0] to STOP: 1 Please Enter a string to store in our database: remove me Enter [1] to add another string to the data base. OR Enter [0] to STOP: 1 Please Enter a string to store in our database: Don't remove me Enter [1] to add another string to the data base. OR Enter [0] to STOP: 1 Please Enter a string to store in our database: Compressed! Enter [1] to add another string to the data base. OR Enter [0] to STOP: 0 ================================================================== MENU ================================================================== 1) Enter 1 to search for a substring. 2) Enter 2 to remove a string from the database. 3) Enter 3 to print the database. 4) Exit Program. Enter: 2 What data entry would you like to delete... ================================================================== This is what we have in our database so far... ================================================================== 1) By tyler 2) remove me 3) Don't remove me 4) Compressed! Please enter the number for the string that you'd like to delte: 2 Working... Deleting... ================================================================== Here is our updated database... ================================================================== 1) By tyler 2) Don't remove me 3) Compressed! We removed: remove me Press 1 and enter to build a new database. Press any other number to exit the program.