How to Delete Word in Dictionary

this is my code.there is a problem in my delete function.how can i delete word from dictionary.i need code for this.

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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
using namespace std;

int option;

class Dictionary
{
public:

	string Words;
	string Meanings;

	Dictionary():Words("NULL"),Meanings("NULL"){}

	void Display_Full_Dictionary();
	void Search();
	void Add_Word();
	void Delete_Word();

}d;

void Dictionary::Display_Full_Dictionary()
{
	int i = 1;

	ifstream in("Dictionary.txt");

	if(!in)
	{
		cout<<"File Not Found...\n";
		return;
	}

	cout<<"\n\n\t\t Words \t\t\t Meanings\n";
	while(in>>Words>>Meanings)
	{
		cout<<"\n\n\t\t "<<Words<<" \t\t\t "<<Meanings<<"\n\n";
		i++;
	}

	in.close();

	if(i == 1)
	{
		cout<<"\nDictionary is Empty...\n\n";
		return;
	}
}

void Dictionary::Search()
{
	int i = 1;
	int j = 1;

	string word;

	cout<<"Enter Word = ";
	fflush(stdin);
	getline(cin,word);

	ifstream in("Dictionary.txt");

	if(!in)
	{
		cout<<"File Not Found...\n";
		return;
	}

	while(in>>Words>>Meanings)
	{
		if(word==Words)
		{
			cout<<"Meaning = "<<Meanings<<"\n\n";
			i++;
		}
		j++;
	}

	in.close();

	if(j == 1)
	{
		cout<<"Dictionary is Empty...\n";
		return;
	}

	if(i == 1)
	{
		cout<<"Word Not Found...\n";
		return;
	}
}

void Dictionary::Add_Word()
{
	ofstream out("Dictionary.txt",ios::app);

	if(!out)
	{
		cout<<"File Not Found...\n";
		return;
	}

	cout<<"Enter Word = ";
	fflush(stdin);
	getline(cin,Words);
	cout<<"\n";
	cout<<"Enter Meaning = ";
	fflush(stdin);
	getline(cin,Meanings);

	out<<"\n\n\t\t "<<Words<<" \t\t\t "<<Meanings;

	out.close();
}

void Dictionary::Delete_Word()
{
	int i = 1;
	int j = 1;

	string word;

	cout<<"Enter Word = ";
	fflush(stdin);
	getline(cin,word);

	//how can i delete words from dictionary....
	

	if(j == 1)
	{
		cout<<"\nDictionary is Empty...\n\n";
		return;
	}

	if(i == 1)
	{
		cout<<"\nWord Not Found...\n\n";
		return;
	}
}

void mainmenu()
{
	while(1)
	{
		system("cls");
		cout<<"\n\n\t**********D**********I**********C**********T**********\n";
		cout<<"\t*\t\t\t\t\t\t     *\n\tI\t\t      Dictionary\t\t     O \n";
		cout<<"\t*\t\t\t\t\t\t     *\n\t**********N**********A**********R**********Y**********\n\n";
		cout<<"\n\t\t\t   |1|  Display Full Dictionay   \n";
		cout<<"\n\t\t\t   |2|  Search                   \n";
		cout<<"\n\t\t\t   |3|  Add New Word             \n";
		cout<<"\n\t\t\t   |4|  Delete Word              \n";
		cout<<"\n\t\t\t   |5|  Exit                     \n";
		cout<<"\n\n\t\t\t   Enter Option = ";
		cin>>option;

		switch(option)
		{

		case 1:
			{
				system("cls");
				cout<<"\n\n\t**********D**********I**********C**********T**********\n";
				cout<<"\t*\t\t\t\t\t\t     *\n\tI\t\t      Dictionary\t\t     O \n";
				cout<<"\t*\t\t\t\t\t\t     *\n\t**********N**********A**********R**********Y**********\n\n";
				d.Display_Full_Dictionary();
				system("pause");
				break;
			}

		case 2:
			{
				system("cls");
				cout<<"\n\n\t**********D**********I**********C**********T**********\n";
				cout<<"\t*\t\t\t\t\t\t     *\n\tI\t\t        Search     \t\t     O \n";
				cout<<"\t*\t\t\t\t\t\t     *\n\t**********N**********A**********R**********Y**********\n\n";
				d.Search();
				system("pause");
				break;
			}

		case 3:
			{
				system("cls");
				cout<<"\n\n\t**********D**********I**********C**********T**********\n";
				cout<<"\t*\t\t\t\t\t\t     *\n\tI\t\t      Add New Word \t\t     O \n";
				cout<<"\t*\t\t\t\t\t\t     *\n\t**********N**********A**********R**********Y**********\n\n";
				d.Add_Word();
				system("pause");
				break;
			}

		case 4:
			{
				system("cls");
				cout<<"\n\n\t**********D**********I**********C**********T**********\n";
				cout<<"\t*\t\t\t\t\t\t     *\n\tI\t\t      Delete Word \t\t     O \n";
				cout<<"\t*\t\t\t\t\t\t     *\n\t**********N**********A**********R**********Y**********\n\n";
				d.Delete_Word();
				system("pause");
				break;
			}

		case 5:
			{
				system("cls");
				exit(0);
				break;
			}

		default:
			{
				cout<<"Wrong Option...Enter Again..."<<endl;
				system("pause");
				break;
			}
		}
	}
}

void main()
{
	mainmenu();
	system("pause");
}
You have to copy the entire text file, leaving out the word you want to delete, and then delete the original Dictionary.txt file and rename the copy to Dictionary.txt

As an aside, this void main() is not C++ and this system("pause"); is expensive, dangerous and non-portable.
Last edited on
thanks for reply.

but i need the code for this.
but i need the code for this.


Write it. It's not difficult. You have to read each word in, and if it's not the word you want to delete, write it out again into the new file. Your code above already reads and writes files, so you shouldn't have any trouble.
Last edited on
i done this but its not working.kindly check this and correct this.

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
void Dictionary::Delete_Word()
{
	int i = 1;
	int j = 1;

	string word;

	cout<<"Enter Word = ";
	fflush(stdin);
	getline(cin,word);

	ifstream in("Dictionary.txt");

	if(!in)
	{
		cout<<"File Not Found...\n";
		return;
	}

	while(in>>Words>>Meanings)
	{
		if(word==Words)
		{
			Words = "";
			Meanings = "";
			i++;
		}
		j++;
	}

	in.close();

	if(j == 1)
	{
		cout<<"\nDictionary is Empty...\n\n";
		return;
	}

	ofstream out("Dictionary.txt",ios::out);
	out<<"\n\n\t\t "<<Words<<" \t\t\t "<<Meanings;

	out.close();

	if(i == 1)
	{
		cout<<"\nWord Not Found...\n\n";
		return;
	}
}
You have to read the file to a vector/list or write them as soon as you read. The way you are doing it, you only get the last word in Word (line 20).
what's the solution for my code.
The one I just pointed out?
i need the solution for my code.
tell me how i can do.
The way you are doing it:
- Read each line from the file to a variable. At the end of the loop, you have the last line on this variable.
- Write to the file this single variable.

The way you should do it:
- Create a vector.
- Read each line from the file to a variable. Check if it is your word. If it's not, add it to the vector.
- Clear the file and write your vector to it.
i need the code for this.i dont understand vertor.plz give me the code for this problem.
i dont understand anything.plz give me code.
give me code plz
Topic archived. No new replies allowed.