-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove.cc
More file actions
132 lines (83 loc) · 2.44 KB
/
remove.cc
File metadata and controls
132 lines (83 loc) · 2.44 KB
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
/*
The purpose of this program is to remove a word from the dictionary.csv file.
*/
#include <iostream>
#include <fstream>
#include <string>
#include <set>
using namespace std;
void loadDictionary ( set<string>& dictionary, const string& filename );
void writeDictionary ( set<string>& dictionary, const string& filename );
void removeFromDictionary ( set<string>& dictionary );
int main() {
const string filename = "dictionary.csv";
set<string> dictionary;
loadDictionary ( dictionary, filename );
removeFromDictionary ( dictionary );
writeDictionary ( dictionary, filename );
return 0;
}
void loadDictionary ( set<string>& dictionary, const string& filename ) {
ifstream inFile;
inFile.open( filename.c_str() );
if ( ! inFile.good() ) {
cerr << " > Error: Failed to open " << filename << endl;
return;
}
string buffer;
string word;
do {
inFile >> buffer;
for ( unsigned int i = 0; i < buffer.size(); i++ ) {
if ( buffer[i] >= 'A' && buffer[i] <= 'Z' ) {
buffer[i] += 32;
word += buffer[i];
} else if ( buffer[i] >= 'a' && buffer[i] <= 'z' ) {
word += buffer[i];
} else if ( buffer[i] == ',' ) {
dictionary.insert( word );
word = "";
}
}
dictionary.insert( word );
word = "";
} while ( ! inFile.eof() );
inFile.close();
}
void writeDictionary ( set<string>& dictionary, const string& filename ) {
ofstream outfile;
outfile.open( filename.c_str() );
if ( ! outfile.good() ) {
cerr << " > Error: Failed to open " << filename << ". " << endl;
exit(3);
}
for ( set<string>::iterator iter = dictionary.begin(); iter != dictionary.end(); iter++ ) {
outfile << endl;
for ( unsigned int i = 3; i < iter->size(); i++ ) {
outfile << ",,";
}
outfile << *iter;
}
outfile.close();
}
void removeFromDictionary ( set<string>& dictionary ) {
string target = "";
while ( true ) {
cout << "Enter word to remove: ";
cin >> target;
if ( target == "0" ) {
return;
}
set<string>::iterator iter = dictionary.find( target );
if ( iter == dictionary.end() ) {
cout << " > " << target << " not found in dictionary. " << endl;
continue;
}
cout << "Are you sure you want to remove '" << target << "'?: ";
char answer;
cin >> answer;
if ( answer == 'y' || answer == 'Y' ) {
dictionary.erase( iter );
}
} // end while ( target != "0" )
} // end removeFromDictionary