help with implementation

This is a discussion on help with implementation within the C++ Programming forums, part of the General Programming Boards category; After I wort this program and it runs really good, : I got a question... How could I impement new ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Lightbulb help with implementation

    After I wort this program and it runs really good, :
    I got a question...
    How could I impement new subtraction operator in order to write this:

    string x = "Idontlikeprograming";
    string y = x - "dont";
    cout<< y; //the result is : " Ilikeprograming"

    So fare I wrote this program:

    ---------------------
    You can run it... I got the idea.. but I need to write it as shown up
    ---------------------
    #include <iostream>
    #include <string>

    string del1(string sor, string dis){
    string temp;
    string::iterator sor_ptr = sor.begin();
    while(sor_ptr <= sor.end()) {
    bool found = false;
    string::iterator dis_ptr = dis.begin();
    for(; dis_ptr<=dis.end(); dis_ptr++) {
    if (*sor_ptr == *dis_ptr){
    found = true;
    }// if
    }// for
    if (!found) temp+= *sor_ptr;
    sor_ptr++;
    }// While loop

    return temp;

    }// del1

    string operator - (string & left, string & right){
    // catenate two string values, forming a new string

    string res= del1( left, right);
    return res; // return result
    // we can also writ here the body of del1 function
    }


    int main(){
    /* string s1= " thisisnotfun";
    string s2= s1 - "not";
    cout<< s2<< endl; */
    string source;
    cout<< "Please inter the first string"<<endl;
    getline(cin, source);
    string dist;
    cout<< "Please inter the second string"<<endl;
    getline(cin,dist);
    string edit1 = del1(source, dist);
    cout << edit1 << endl;

    return 0;
    }

    Thankx Guys...
    Last edited by NANO; 04-29-2002 at 02:16 PM.
    C++
    The best

  2. #2
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    if you use spaces you can use strtok(maybe, might only be for char arrays) to tokenize words into a temp string and then check if the temp is the word you want to remove. If it isnt add it to the real string, if it is, ignore and continue.

    And pleeeeeeeease use code tags!!!!!!!!

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Can you go farther than that ...

    hi ErionD,
    Please can you discribe it in more details...

    Please tell me more about it ...
    C++
    The best

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    I told you before that I will never help you if you don't use code tags.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Ok sorry

    I will use it ...
    but please help me...

    #include <iostream.h>
    #include <string.h>

    Like this

    Help me please ...
    C++
    The best

  6. #6
    www.entropysink.com
    Join Date
    Feb 2002
    Posts
    605
    No.

    Submit the code again with code tags.
    Visit entropysink.com - It's what your PC is made for!

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Thumbs up Ok here we go....

    string x = "Idontlikeprograming";
    string y = x - "dont";
    cout<< y; //the result is : " Ilikeprograming"

    So fare I wrote this program:

    ---------------------
    You can run it... I got the idea.. but I need to write it as shown up
    ---------------------
    #include <iostream.h>
    #include <string.h>

    string del1(string sor, string dis){
    string temp;
    string::iterator sor_ptr = sor.begin();
    while(sor_ptr <= sor.end()) {
    bool found = false;
    string::iterator dis_ptr = dis.begin();
    for(; dis_ptr<=dis.end(); dis_ptr++) {
    if (*sor_ptr == *dis_ptr){
    found = true;
    }// if
    }// for
    if (!found) temp+= *sor_ptr;
    sor_ptr++;
    }// While loop

    return temp;

    }// del1

    string operator - (string & left, string & right){
    // catenate two string values, forming a new string

    string res= del1( left, right);
    return res; // return result
    // we can also writ here the body of del1 function
    }


    int main(){
    /* string s1= " thisisnotfun";
    string s2= s1 - "not";
    cout<< s2<< endl; */
    cout<< " I did submet the tags with the code"<< endl;
    cout<<"can you help me now"<< endl;
    string source;
    cout<< "Please inter the first string"<<endl;
    getline(cin, source);
    string dist;
    cout<< "Please inter the second string"<<endl;
    getline(cin,dist);
    string edit1 = del1(source, dist);
    cout << edit1 << endl;

    return 0;
    }
    Last edited by NANO; 04-30-2002 at 02:00 PM.
    C++
    The best

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    To use a code tag, precede your code with "code" and follow it with "/code". Replace the quotation marks with [ and ]. Thank you.
    Truth is a malleable commodity - Dick Cheney

  9. #9
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    use [ CODE] without the space before the code and then the same but with a / before CODE after it. Is it really so hard????

    Anyway:

    Code:
    char string[50];
    char newstring[50];
    char temp[20];
    
    strcpy(string,"String with spaces");
    temp = strtok(string," ");
    
    if(strcmp(temp,"with")!=0) {
    strcat(newstring,temp);
    strcat(newstring," ");
    }
    temp = strtok(NULL," ");
    Then just put it all in a loop.

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Talking Is this OK... As a Code Tage

    Code:
    #include <iostream.h> 
    #include <string.h> 
    
    string del1(string sor, string dis){ 
     string temp; 
     string::iterator sor_ptr = sor.begin(); 
    
     while(sor_ptr <= sor.end()) { 
     bool found = false; 
     string::iterator dis_ptr = dis.begin(); 
     
     for(; dis_ptr<=dis.end(); dis_ptr++) { 
     if (*sor_ptr == *dis_ptr){ 
     found = true; 
     }// if 
     }// for 
    
     if (!found) temp+= *sor_ptr; 
     sor_ptr++; 
     
     }// While loop 
    
    return temp; 
    }// del1 
    
    string operator - (string & left, string & right){ 
    // catenate two string values, forming a new string 
    
     string res= del1( left, right); 
     return res; // return result 
    // we can also writ here the body of del1 function 
    } 
    
    
    int main(){ 
       /* string s1= " thisisnotfun"; 
       string s2= s1 - "not"; 
       cout<< s2<< endl; */ 
    
    string source; 
    string dist;
    string edit1; 
     
    cout<< " I did submet the tags with the code"<< endl; 
    cout<<"can you help me now"<< endl; 
    
    cout<< "Please inter the first string"<<endl; 
    getline(cin, source); 
    
    
    cout<< "Please inter the second string"<<endl; 
    getline(cin,dist); 
    
    edit1 = del1(source, dist); 
    cout << edit1 << endl; 
    
    return 0; 
    }
    Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaha
    Last edited by NANO; 04-30-2002 at 02:06 PM.
    C++
    The best

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    I am not allow to use these functions...

    guys thanks for your help ...
    You know as a bigner, I am here to learn ... and Sorry for making problems... but the thing that I want to say is : " I am not allow to use the string function that came with thew string lib."
    I want to implement a new operator to add it to the string lib functions and operators.....
    It is very easy to use the strtok and strcpy and strcat...

    According to the Prof. I solve it in a right way ... but he want me to implement the "-" operator... insted of using the Del1 function ...
    and I don't know how to implement this operator using the C++ syntax.

    Can you do it for me.... ?
    C++
    The best

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    you write an overloaded operator.
    change your function Del1 declaration to this
    Code:
    friend string operator-(string sor ,string dis);
    I don't know if i got the syntax or whatever right (I don't have any materials to look it up and I'm to lazy too anyways) but you can look it up in the help files. But that should help point you in the right direction (look up : operator overloading, friend, operator.

    cheers
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

  13. #13
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    So this is the header of the - opertaor

    Can I say that is the header of the " -" operator...
    and inside it I should write what I did before...

    Code:
    friend string operator-(string sor ,string dis);
    Question: Why it is Firend ?

    And is this the way to write Using a Code tage right ... Tell me ?
    Thanx...
    you know step by step.... this is the way ...
    Last edited by NANO; 04-30-2002 at 02:06 PM.
    C++
    The best

  14. #14
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    How to do code tags 101 (follow the steps) words in " " is what you type.

    1 - "["
    2 - same line.. "code]"
    3 - put in your code here...
    4 - "["
    5 - same line.. "\code]"

    friend means it can be used by classes (ie string) if I remember correctly but look it up in the help it should say I don't have any of that here with me so I can not look it up.
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

  15. #15
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    I got it ,.....

    Now I got it ...

    to much Wiscky
    Now I know how to do this ...
    C++
    The best

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 01:18 PM
  2. Replies: 1
    Last Post: 08-24-2008, 12:39 PM
  3. implementation file
    By bejiz in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2005, 01:59 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. Pure virtual implementation, or not.
    By Eibro in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2003, 08:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21