I'm trying to find the word "language" in my text file (after that I'll have to do some further processing) but right now my console window displays "found" every other word
1 2 3 4 5 6 7 8 9 10 11 12
int main() {
string raw, headL[2], lang[2];
ifstream data("testsample.txt");
while (data.good()) {
data >> raw;
if (raw.find("language")) { cout << "found"; }
cout << raw;
}
return 0;
}
Usually is 0 that is interpreted as 'false' and std::string.find() never returns 0.
Actually it can return zero, but only if the "to find string" occurs at the first element of the string. Ie:
1 2 3 4 5 6 7 8 9 10 11
#include <string>
#include <iostream>
int main()
{
string raw{"language test for language"};
std::cout << raw.find("language");
return 0;
}
But if the find fails to find the string it returns std::string::npos, which is the largest possible value that can be held in a std::string::size_type, not zero as you have correctly pointed out.