May 23, 2013 at 4:44am UTC
Hi there,
I need a little help with my project.
I want the user to enter the word and I want to check if it exists in a text file or not.
I tried
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
int flag=0;
char word[50], input[50];
cout<<"Enter word to search for" ;
gets(input);
ifstream myfile;
myfile.open("Dictionary.txt" );
while (myfile>>word)
{
if (word==input)
{
flag=1;
break ;
}
}
myfile.close();
if (flag==0)
cout<<"Not found" ;
if (flag==1)
cout<<"Found" ;
but it does not seem to be working. Should I make changes to it or is there another way?
Thanks
Last edited on May 23, 2013 at 1:18pm UTC
May 23, 2013 at 5:40am UTC
what are types of word and input?
May 23, 2013 at 12:43pm UTC
It doesn't seem likely they're both of type char.
Perhaps they are of type array of char (or pointers to the same?) In which case, line 8 is comparing the address of word to the address of input, and since they aren't at the same address, the condition will never evaluate to true.
Last edited on May 23, 2013 at 12:44pm UTC
May 23, 2013 at 1:14pm UTC
My bad. Both are indeed char arrays.
May 23, 2013 at 3:31pm UTC
Ah! That's done the trick. Thanks a ton :)