explain .find

I am trying to say that if a sentence contains the right word it will print "correct", so for example a user could say "the fibonacci series" or "the fibonacci sequence" and both would be correct because the answer contains the word "fibonacci" but I pretty much copied the code below from somewhere else and changed it a bit. Could someone please explain what this line is doing in simple terms. "bool found = answer.find("fibonacci") != string::npos;"
Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 cout << "What is the name of this series of numbers?" << endl;
string answer;
getline(cin, answer);

bool found = answer.find("fibonacci") != string::npos;
	if(found == true)
	{
	cout << "correct" << endl;
	}

	else 
	{
	cout << "incorrect" << endl;
	}
Did you read the reference page for string::find?
http://www.cplusplus.com/reference/string/string/find/?kw=string%3A%3Afind

Searches the string for the first occurrence of the sequence specified by its arguments.
So if answer contains "fibonacci", find returns the position of the desired string.
If answer does not contain "fibonacci", find returns string::npos (usually -1).
Line 5 compares the return value of find to see if it is not equal to string::npos.
If the returned value is not equal to string::npos (a match was found), then the result of the boolean expression is true and is stored in found. If no match was found, then the found is set to false.

Topic archived. No new replies allowed.