Reputation: 11
How are the word files stored in a text file.
I am trying to take a word as input from and comparing that word with the words present in my local path.
how to compare the input word to the words in the file.
#include <iostream>
#include <fstream>
using namespace std;
string str;
fstream file;
string word, t, q, filename;
void read()
{
cout << "Enter the word to search in the dictionary:";
cin >> str;
cout << "Your Entered word is :" << str;
cout << "\nsearching in the librabry(Dictionary).";
return;
}
void search()
{
//string str;
filename = "da1.txt";
file.open(filename.c_str());
if (str == word)
{
cout << "Found" << word << endl;
}
else
{
cout << "\nEntered word does not exist in the library." << endl;
}
return;
}
int main()
{
search();
}
I am pretty sure that my word is not being compared to the words in the text.
Upvotes: 0
Views: 80
Reputation: 9
Your are not calling your method read()
, so your var word
is null
.
Secondly, it seems you want to know if str
contain word
, instead of a simple equal. Check out this link.
As noted @Paul Sanders, you are not reading data from your file
Upvotes: 0
Reputation: 48258
Your issue is here:
void read()
{
cout<<"Enter the word to search in the dictionary:";
cin>>str;
cout<<"Your Entered word is :"<<str;
cout<<"\nsearching in the librabry(Dictionary).";
return;
}
and is caused because:
read()
method is never called,if(str==word)
so you compare 2 non initialized objectsstd::string str{};
Upvotes: 1