Trouble with a function!

I am working on my C ++ homework. The goal is to create a function which will check a given file for a particular word, and then return the number of times the word appears in the file back to main. I am stuck, no entirely sure what I am doing wrong, but i haven't dealt with fstreams much yet. It is homework so I don't want an "answer" but just a fresh set of eyes to maybe give a hint as to what I am missing.

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
26
27

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

int count_the_word(string filename, string word)
{
    int word_count = 0;

    ifstream data_store;

    string check;
    data_store.open( filename );

    while (!data_store.eof())
    {
        data_store >> check;

        if ( check == word )
        {
            word_count ++;
        }

    }
    return word_count;
}
You forgot to tell what is your problem. Is it that sometimes your function returns one more than it should?
H:\cs111\word_search.cpp|12|error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&)'|
is the error message codeblocks gives me
Turn on C++11 support
Settings→Compiler→Compiler Flags→Check Have g++ follow the C++11 ISO C++ language standard
Nice. That does allow it to run, and i am getting the correct numbers, however when aI try to turn it into hypergrade I get E:\temp\76A0.tmp\/word_search.cpp: In function 'int count_the_word(std::string, std::string)':

E:\temp\76A0.tmp\/word_search.cpp:12:29: error: no matching function for call to 'std::basic_ifstream::open(std::string&)'

c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/fstream:526:7: note: candidate is: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char, _Traits = std::char_traits, std::ios_base::openmode = std::_Ios_Openmode]
Ok, to support outdated compilers you need to do: data_store.open( filename.c_str() );

Also change
1
2
3
    while (!data_store.eof())
    {
        data_store >> check;
to
1
2
    while (data_store >> check)
    {
Do notloop on eof(). It is wrong and will lead to problems.
Check on following file:
food  
drink  
food  
and word "food"

Yes, this was the problem, ironically I found it seconds before you sent the message. I very much appreciate your time this was becoming very frustrating. It seems to work fine as coded, what kind of problems are you referring to if I don't change it ?
Did you try file I posted?
With some conditions being true, it will report 3 entries of word "food".
(make sure there is an empty line or space at the end of file)
I will experiment with it tommorow to see if I can trip it up, thanks again for the help, and have a great day!
Here is an example of this error:
http://ideone.com/pfuEEi
Topic archived. No new replies allowed.