sorry if this has been asked before, but i am confused about a code which is posted on the tutorial page:
why do i have to use loops to use the find function? i tried using if and it still works; it even gives the same output! here's the code which i modified from the tutorial using if:Code:string input; int i = 0; int cat_appearances = 0; getline(cin, input, '\n'); for(i = input.find("cat", 0); i != string::npos; i = input.find("cat", i)) { cat_appearances++; i++; // Move past the last discovered instance to avoid finding same // string } cout<<cat_appearances;
can someone explain it to me? thanks in advance though ! ^^Code:#include<iostream> #include<Windows.h> #include<string> using namespace std; int main() { string input; int i = 0; int cat_appearances = 0; if(input.find("cat", 0) != string::npos) cat_appearances++; cin.get(); return 0; }



2Likes
LinkBack URL
About LinkBacks




