Pointer troubles

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ShowDown
    New Member
    • Sep 2006
    • 2

    Pointer troubles

    im recently working on a project, where I need to read one line, check it for data, then delete some of the data in the string, im using substr. The problem comes when I have to do it in functions.
    sorry if i confuse you, i'll try give a example.
    FX.
    Code:
    int DoSome(string *inp) {
    if (inp.substr(0,6)=="myflow") {   // here's my problem, inp only points, and don't got the method substr.
    inp = inp.substr(6);
    // lot of code here
    }
    }
    
    int main() {
       string hello;
       hello = "myflow123";
       DoSome(&hello);
    }
    Sorry its not snips, becusase its too big and this would really simplify it, plus make it faster.

    Regards
  • Jai Vrat Singh
    New Member
    • Oct 2006
    • 18

    #2
    I am not sure if you are saying the it soes not gets substr method of std::string class , but effect you are trying to achive can be done by

    Code:
    int DoSome(string *inp) {
    if ((*inp).substr(0,6)=="myflow") {   // here's my problem, inp only points, and don't got the method substr.
    (*inp) = (*inp).substr(6);
    // lot of code here
    }
    return (0);
    }
    
    int DoSome(string &inp) {
    if (inp.substr(0,6)=="myflow") {   // here's my problem, inp only points, and don't got the method substr.
     inp = inp.substr(6);
    // lot of code here
    }
    return (0);
    }
    
    
    int main() {
       string hello;
       hello = "myflow123";
       std::cout<<" Before hello is >>"<<hello<<"<<\n";
       DoSome(&hello);
       std::cout<<" After hello is >>"<<hello<<"<<\n";
    
       string junk;
       junk = "myflow123";
       std::cout<<" Before junk is >>"<<junk<<"<<\n";
       DoSome(junk);
       std::cout<<" After junk is >>"<<junk<<"<<\n";
    
    
    }
    Code you gave takes a poiter as input and you are using . operator , which will give you compile time errors. You can modify it as above or the best would be use references like

    Code:
    int DoSome(string &inp) {
    if (inp.substr(0,6)=="myflow") {   // here's my problem, inp only points, and don't got the method substr.
     inp = inp.substr(6);
    // lot of code here
    }
    return (0);
    }

    Comment

    • ShowDown
      New Member
      • Sep 2006
      • 2

      #3
      Thanks.
      Thats exactly what I needed :D
      Now my project finaly can continue

      regards

      Comment

      Working...