aboutsummaryrefslogtreecommitdiff
path: root/num2.cpp
blob: 0b028e9c51e2226c9dd16afa83a29cf20d719931 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// tyler taormina
// cst 116
// lab 7 
// number 1

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void RmString(string arr[]);
void GatherStrings(int& limit, string arr[]);
void ClearBuffer();
void DisplayMenu(int&);
void ProcessMenuChoice(int, int, string arr[]); 
void PrintString(int, string arr[]);
bool 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();
   }
}


bool FindString(string arr[], int limit)
{
   // Check for a substring inside of one of the input strings.

    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())
            return false;
        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()){
                    return true;
                }
                else
                    j = reset;
            }
        } 
    }
    return false;
}


void PrintString(int limit, string arr[])
{
    int i = 0;
    cout << "==================================================================\n";
    cout << "This is what we have in our database so far..." << endl;
    cout << "==================================================================\n";
    
    while (i < limit)
    {
        cout << *(arr+i) << endl;
        i += 1;
    }

}


void RmString(string arr[])
{

}


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[])
{
    int program_rerun = 0;
    bool check;
    switch(menu_choice)
    {
        case 1: 
            check = FindString(usr_data, limit);
            if (check) {
                cout << "WE FOUND IT!" << endl;
            }
            else
                cout << "ME NO FIND" << endl;

            break;
        
        case 2: 
            RmString(usr_data);
            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);
}