aboutsummaryrefslogtreecommitdiff
path: root/CST116F2021-Lab7/Functions.cpp
blob: 5e3335e0a923b127fa68f92265fb63346ced9309 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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--; 
}