Here I have a function that suppose search a stack and return the substring if found the match otherwise return Not exist. However, it somehow always return Not exist. I don't see why. Could someone please help me out? Thank you in advance.
It return Not exist with this mainCode:string stack_search(stack<string> column, string target, string taq) { string temp; string::size_type P_loca; string::size_type E_loca; while(!column.empty()) { temp=column.top(); column.pop(); if (temp.find(target)!=string::npos && temp.find(taq) != string::npos) { P_loca = temp.find("P"); E_loca = temp.find("E"); return temp.substr(P_loca, E_loca-P_loca); } else {return "Not exist";} } }
Code:int main(int argc, char *argv[]) { stack<string> A; A.push("Times22 P25 ELQ"); A.push("Times22 P6 ETQ"); A.push("Times3 P7 ELQ"); cout << stack_search(A, "Times22","ETQ") <<endl; system("PAUSE"); return EXIT_SUCCESS; }



LinkBack URL
About LinkBacks


