blob: d454d9233de6f043a393b094721eb210a989f498 (
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
|
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 <iostream>
#include <iomanip>
#include <string>
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\nThis is what we have in our database so far...\n\n" << endl;
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: this is sting 3
Enter 1 to add another string to the data base.
OR
Enter 0 to stop and see all the entries we have so f 1 u Please Enter a string to store in our database: How many more times should I do this?
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: Ill do 2 more
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: One moreE!
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: I'm done!
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
this is sting 3
How many more times should I do this?
Ill do 2 more
One moreE!
I'm done!
===============================================================================
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:
RUN:
==============================================================================
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:
RUN:
|