Comparing words with contents of a text file

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
what are types of word and input?
Both are char type.
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
My bad. Both are indeed char arrays.
On line 12 you're comparing pointers not the content. The pointers are always different.

use strcmp():

http://www.cplusplus.com/reference/cstring/strcmp/?kw=strcmp
Ah! That's done the trick. Thanks a ton :)
Topic archived. No new replies allowed.