0

Basically what my code does is compare the words in keywords.txt file with the words in the keywords array and shows error suggestions based on whether the distance between these words is 1 or 2 and when these two words are not the same. *I am unable to figure out why it is still showing the suggestion for words that are the same*? Any suggestions?

Below is my code

#include <iostream>
#include <fstream>
#include <string>
#include<vector>
using namespace std;

int EditDistance(string word1, string word2);

int main ()
{
    //keywords provided.
    string keywords[24] ={"auto","break","case","char","const","continue","default","do","double","else","enum","extern","float","for","goto","if","int","long","return","short","struct","switch","void","while"};
    int loop=0; //short for loop for input
    string line; //this will contain the data read from the file
    int numLines = 0;
    string unused;
    int result;
    ifstream myfile ("keywords.txt"); //opening the file.

    string arr[200];
     if (myfile.is_open()) //if the file is open
    {
        while (! myfile.eof() ) //while the end of file is NOT reached
        {
            getline (myfile,line); //get one line from the file

            arr[loop] = line;
          //  cout << arr[loop] << endl; //and output it
            loop++;
        }
        myfile.close(); //closing the file
    }
    else cout << "Unable to open file"; //if the file is not open output
  /*
    for(int i=0;i<24;i++)
    {
        for(int j=0;j<loop;j++)
        {
        if(arr[j]==keywords[i])
            cout<<arr[j]<<" and "<<keywords[i]<<" match. "<<endl;

        }
    }*/

    cout<<endl<<endl;
    cout<<"################## ERROR SUGGESTIONS ################"<<endl;
    cout<<"#                                                   "<<endl;
    cout<<"#                                                   "<<endl;

    for(int i=0;i<24;i++)
    {
            for(int j=0;j<loop;j++)
        {

            result=EditDistance(arr[j],keywords[i]);
            if (result==1 || result==2 && (arr[j]!=keywords[i]))    
            cout<<"# Use "<<keywords[i]<<" instead of " <<arr[j]<<" ? "<<endl;


        }
    }
    cout<<"#                                                   "<<endl;

    cout<<"#"<<endl;
    cout<<"#####################################################"<<endl;
    system("pause");
    return 0;
}

int EditDistance(string word1, string word2)  //function to find the distance between two words.
{
    int i, j, l1, l2, m;
    l1 = word1.length();
    l2 = word2.length();
    vector< vector<int> > t(l1 + 1, vector<int>(l2 + 1));

    for (i = 0; i <= l1; i++)
        t[i][0] = i;
    for (i = 1; i <= l2; i++)
        t[0][i] = i;

    for (i = 1; i <= l1; i++)
    {
        for (j = 1; j <= l2; j++)
        {
            m = min(t[i-1][j], t[i][j-1]) + 1;
            t[i][j] = min(m, t[i-1][j-1] + (word1[i-1] == word2[j-1] ? 0 : 1));
        }
    }
    return t[l1][l2];
}
2
  • Have you verified the strings are really identical? There isn't a '\r' on the end, is there? Commented Feb 28, 2014 at 20:05
  • My suggestion is you step through your code in the debugger - the answer will not only be obvious but you won't get very far learning C++ without learning how to use one. Commented Feb 28, 2014 at 20:09

1 Answer 1

1

On the line

if (result==1 || result==2 && (arr[j]!=keywords[i]))    

You probably mean

if( (result==1 || result==2 ) && arr[j]!=keywords[i])    

The first version is the same as

if (result==1 || (result==2 && arr[j]!=keywords[i]))   

So you will output if the result is 1 even if the word is correct. Which is not what you want.

Sign up to request clarification or add additional context in comments.

3 Comments

The key point being that logical AND gets evaluated before logical OR. en.cppreference.com/w/cpp/language/operator_precedence
@AndyG Thanks for adding that, I accidentally submitted before I was done writing it, I was going to include that as well.
I tried it by doing " if( (result==1 || result==2 ) && arr[j]!=keywords[i]) " as well as i tried if( (result==1 || result==2 ) && (arr[j]!=keywords[i])) but still it shows " Use for instead of for ? " in one of the output lines.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.