String Finding

Having my string inputted by the user. How would I find the position of the word the user entered? For example, user entered "How now cow" and they want me to find "cow" which would be position 3. I keep getting -1.

Some of my code, terribly wrong:
1
2
3
4
5
6
7
8
 cout << "\nWhat piece of the string do you want to find?\n";
    int look, pos;
    cin >> look;
    pos = input.find(look, 0);

    if (pos != string::npos){
        cout << look << "found !" << endl;
    }
You have look as an int - shouldn't it be a string?

Find takes iterators as the first 2 arguments, the string as the 3rd.

http://www.cplusplus.com/reference/algorithm/find/


Even though the example is an array of ints, you can use it for strings.

HTH
Topic archived. No new replies allowed.