Bhavani Shankar
Bhavani Shankar

Reputation: 11

String Comparision

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

Answers (2)

Paul
Paul

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

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:

  1. read() method is never called,
  2. you ask for a word to search but assign that input to str variable
  3. in search method you do if(str==word) so you compare 2 non initialized objects
  4. you should do something like std::string str{};

Upvotes: 1

Related Questions