Word recognition

1
2
3
4
5
6
7
8
9
10
11
    string word="";
    std::ifstream in("Testing.txt");
    std::string contents((std::istreambuf_iterator<char>(in)), 
    std::istreambuf_iterator<char>());
    cin>> word;
    if(word == contents) {
                cout<< "\nRecognized.\n";
                getch(); }
    else {
         cout<< "\nNot recognized.\n"; 
         getch(); }


I am trying to get the program to compare the contents of the text file to the string input by the user. I get "Not recognized" every time.
Please help, this is for a bigger project due soon.
I know that this is most likely the worst code you'll see in a while but I'm learning :S
Have you run it through a debugger and checked you're getting suitable values for 'contents'? If you don't have a debugger, simple cout statements to see what you're comparing will help.

I would think your problem is going to be that you're not really reading the content of the "Testing.txt" file (it's either missing, or in a different location), or that you're getting LF characters at the end that you're not allowing for.

Jim
Thanks, the problem was that I had an extra space in the textfile. :S

I fixed it and it works :D

As for LF characters, what are they?
LF characters are 'line feeds'/newline characters, the characters that are represented by '\n' in character strings.
Ok thanks, I'll remember that.
Topic archived. No new replies allowed.