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...
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:
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.