File analyzing.

Hi, i want to create a program that read a file and count how many word "cpp" in the file..

for example

1
2
3
4
5
//this in file 'Sample.dat'
cpp is the a
test word cpp
abc cpp 123
//this in file 'Sample.dat' 


this is the code im woring with.

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
28
29
30
31
32
33
34
#include <fstream>
#include <iostream>
#include <string>
 
int main()
{
    using namespace std;
 
    // ifstream is used for reading files
    // We'll read from a file called Sample.dat
    ifstream inf("Sample.dat");
 
    // If we couldn't open the output file stream for reading
    if (!inf)
    {
        // Print an error and exit
        cerr << "Uh oh, Sample.dat could not be opened for reading!" << endl;
        exit(1);
    }
 
    // While there's still stuff left to read
    while (inf)
    {
        // read stuff from the file into a string and print it
        std::string strInput;
        inf >> strInput;
        cout << strInput << endl;
    }
 
    return 0;
 
    // When inf goes out of scope, the ifstream
    // destructor will close the file
}


i tried to put while after inf >> strInput; line to create a counter but it cannot work with string 'cpp'

how to do this? thanks
if (strInput == "cpp") counter++; I don't see what the problem is.
silly mistake, i use ' instead of "
:P
thanks
Topic archived. No new replies allowed.