Thread: strange problem - string manipulation [fixed]

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    22

    Red face strange problem - string manipulation [fixed]

    Alright, here's the problem. I have a simple function that takes an input string, a starting delimeter and an ending delimeter, and returns the string in between.

    Code:
    std::string getBetween(std::string buffer, std::string first, std::string second)
    {
         std::string input = buffer;
         std::string output;
                
         int fpos = input.find(first, 0);
         int spos = input.find(second, fpos);
                
         fpos += 1;
         spos -= 1;
         
         int length = (spos - fpos) + 1;
    
         std::cout<<"POS: "<<fpos<<" - "<<spos<<"\n";
         std::cout<<"INPUT: "<<input<<"\n";
    
         output = input.substr(fpos, length);
    
         std::cout<<"OUTPUT: "<<output<<"\n\n";
    
         return output;
    }
    It works fine except for a particular case. This is the output I get:
    Code:
    POS: 4 - 9
    INPUT: var(newvar);
    OUTPUT: newvar
    
    POS: 4 - 35
    INPUT: set(newvar, "testing print function");
    OUTPUT: newvar, "testing print function"
    
    POS: 1 - -1
    INPUT: "testing print function"
    OUTPUT: testing print function"
    
    POS: 6 - 11
    INPUT: print(newvar);
    OUTPUT: newvar
    
    POS: 0 - -2
    INPUT: END;
    OUTPUT: END;
    The problem is in the 3rd section (and the last one too, though that doesn't matter so much). I don't know why this is happening, could it be a problem with the input or is it the function itself? Any help would be appreciated.

    edit: Arghh! I think I just realised the problem within about 10 seconds of posting! I forgot to check if it was found or not!! >.<

    Alright I got it fixed, problem was I was searching for the end string starting from fpos instead of fpos+1!
    Last edited by Spitball; 01-09-2005 at 08:28 AM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Can you post the test driver that gives you that output, please?
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. string pattern search problem
    By goron350 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 08:50 AM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM