error in search...simple dictionary codes..

guyss need help for search....there is no content when i search word in the dictionary.txt....this is my example of dictionary.txt...in the add case it is working,,only in search case...

--------------
dictionary.txt content.
--------------
mwet-asd.
test-test.
apple-prutas.
beryy-berry.
house-house.
--------------

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
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
using namespace std;
int counter=0;
char op;

int main () {
ifstream write("dictionary.txt", ios::in);
ofstream read("dictionary.txt", ios::app);
string word,meaning;

if(!write){
cout<<"file not found!!";
system ("pause");
return 0;
}
cout<<"\t*DICTIONARY*\n";
cout<<"============================";
cout<<"\n\n[a] Add word";
cout<<"\n[s] Search word";
cout<<"\n\n============================";
cout<<"\nEnter operation:";
cin>>op;
cout<<"\n============================\n";
switch (op){ case 'a':

{cout<<"\nEnter word:\n";
cin>>word;
cout<<"\nEnter meaning:\n";
cin>>meaning;

read<<"\n"<<word<<"-"<<meaning<<".";
break;}

case 's':
{ string aword,ameaning;
string add;
cout<<"Enter word to search:";
cin>>add;
if (add==aword){
while (getline(write,aword)) {
aword = add.substr(0, add.find("-"));
add = add.substr(aword.length()+1,aword.length());
ameaning = add.substr(0, add.find("."));}}


cout<<"====================================";
cout<<"\nword:"<<aword<<add<<"\n";
cout<<"\nmeaning:"<<ameaning<<"\n";
cout<<"====================================\n";
system("pause");
getch ();
} 
} 
}
Last edited on
1
2
ifstream write("dictionary.txt", ios::in);
ofstream read("dictionary.txt", ios::app);

There may be problems if you open a file for reading and appending at the same time. Also, it appears that you swapped your variable names: your "write" file is for reading and your "read" file is for writing.

I would do something like the following as opposed to having both streams open at once:

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
case 'a':
{
   //Append case. Open the file for appending
   std::ofstream appendFileStream("dictionary.txt", ios::app);

   if(!appendFileStream)
   {
      //handle problem here
   }

   ...

   appendFileStream<<"\n"<<word<<"-"<<meaning<<".";

   appendFileStream.close();

   break;
}

case 's':
{
   //Search case. Open the file for reading
   std::ifstream searchFileStream("dictionary.txt");

   if(!searchFileStream)
   {
      //handle problem here
   }

   //Do search
   ...

   searchFileStream.close();
   break;
}


1
2
3
4
5
6
7
string aword,ameaning;
string add;
cout<<"Enter word to search:";
cin>>add;
if (add==aword){ // <-- This is a problem
while (getline(write,aword)) {
...


The above if(add==aword) is checking if add is the empty string (because aword hasn't been assigned anything yet). You should get rid of this check because otherwise it would only go through if the user enters nothing.
Last edited on
Topic archived. No new replies allowed.