Cant figure this out

I cannot figure this out, i have a program that opens a text file and searches for the word to find and it does all that but it doesnt show how many instances of the word are in there.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

void findFunction();

int main ()
{
    findFunction();
/*
    string enterText;
    string sFind;
    size_t Instances = 0;
    string::size_type pos = 0;

    cout << "Enter a line of text" << endl;
    getline(cin, enterText);

    cout << "What word do you want to find" << endl;
    cin >> sFind;

    while((pos = enterText.find(sFind, pos)) != string::npos)
    {
        Instances++;
        pos += sFind.size();
    }

    if(Instances == 1)
    {
        cout << "There is " << Instances << " instance of the word " << sFind << endl;
    }
    else if(Instances > 1)
    {
        cout << "There are " << Instances << " instances of the word " << sFind << endl;
    }
    */
}

void findFunction()
{
    ifstream file;
    string strFind;
    string openFile;
    string storeText;
    size_t instances = 0;
    string::size_type position = 0;

    cout << "Open what document?" << endl;
    cin >> openFile;

    file.open(openFile.c_str());

    stringstream ss(stringstream::app | stringstream::out);

    while(getline(file, storeText))
    {
        ss << storeText << endl;
    }

    cin.ignore();
    storeText = ss.str();

    cout << storeText << endl;

    cout << "What do you want to find?" << endl;
    getline(cin, strFind);

    while(position = storeText.find(strFind, position) != string::npos)
    {
        instances++;
        position += strFind.size();
    }
    if(string::npos)
    {
        cout << "Error" << endl;
    }

    cout << instances << endl;
}
bump
= has higher precedence than != so line 71 is seen as
while(position = (storeText.find(strFind, position) != string::npos))
by the compiler. To fix this you have to put parentheses around the assignment.
while((position = storeText.find(strFind, position)) != string::npos)
Topic archived. No new replies allowed.