Thread: Not returning the correct string

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    42

    Not returning the correct string

    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.

    Code:
    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";}
                              
                     }
                }
    It return Not exist with this main
    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;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your else is inside the while, so if the first entry doesn't match, "Not exist" is returned immediately instead of continuing to the next entry.

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    You exit your loop prematurely. If your search fails on the first item, that else kicks in and your loop exits. You never get past the first item.
    Move the "Not exist" to outside of the loop.

    Edit: Beaten. X_X. Also, passing a stack of strings like that seems like a poor idea. You're copying all those strings each time. Is std::stack the right container for whatever you're using?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Returning reference to STL String class
    By frisbee in forum C++ Programming
    Replies: 5
    Last Post: 12-29-2003, 12:39 PM
  4. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM