Like Tree1Likes
  • 1 Post By laserlight

String Copy in C++

This is a discussion on String Copy in C++ within the C++ Programming forums, part of the General Programming Boards category; Hello, I am trying to do a string copy in C++. I have gotten the first one in my code ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    14

    String Copy in C++

    Hello, I am trying to do a string copy in C++. I have gotten the first one in my code to work, but the next one that accesses my classes does not work. The string compares however do.

    Code:
    void Phonebook::delFriend(int * index, Phonebook * pb){
        int i = 0;
        int j = 0;
        string fn;
        string ln;
        char buffer[20];
        string str ("Test...");
    
    
        str.copy(buffer, MAX_NAME_LENGTH);
    
    
        cout<<buffer;
    
    
        cout<<"Firstname: ";
        cin>>fn;
        cout<<"Lastname: ";
        cin>>ln;
        cout<<"...Friend deleted\n";
    
    
    
    
    
    
        for(i = 0; i < * index; i++){
             if((fn.compare(pb[i].get_firstName(i)) ==0) && (ln.compare(pb[i].get_lastName(i)) ==0)){
                for(j = 0; j < *index; j++){
                    pb[j+1].get_firstName(j+1).copy(pb[j].get_firstName(j), MAX_NAME_LENGTH);
                    //strcpy(pb[j].lastName, pb[j+1].lastName);
                    //strcpy(pb[j].homeNumber, pb[j+1].homeNumber);
                    //strcpy(pb[j].cellNumber, pb[j+1].cellNumber);
                }
             }
        }
    
    
       *index -= 1;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    Why don't you just use the std::string objects and their copy assignment operator=?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    14
    Could you please explain a little bit in more depth how I would use it? Perhaps an example?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    Ugh, looking more closely at your code, you are in need of a redesign. Take a look at this line:
    Code:
    void Phonebook::delFriend(int * index, Phonebook * pb){
    Why is index a pointer? You never check for a null pointer, so you might as well use a reference parameter instead. Then, what is the point of pb? This is a member function, but you seem to use pb instead of the *this object. Furthermore, pb appears to be a pointer to the first element of an array. So, you are deleting a friend from an array of phone books, rather than from a phone book?

    Next, I see:
    Code:
    cout<<"Firstname: ";
    cin>>fn;
    cout<<"Lastname: ";
    cin>>ln;
    This does not belong here. Your functions should be designed to do one thing and do it well. Hence, separate the concerns: one function deletes the friend; another function requests for and reads the friend name from the user, and then calls the other function to delete the friend (or maybe another function calls both of them).

    Now, I observe:
    Code:
    str.copy(buffer, MAX_NAME_LENGTH);
    This is completely unnecessary. You already have str. Why copy to buffer when you don't need such a buffer?

    Take a look at this line:
    Code:
    if((fn.compare(pb[i].get_firstName(i)) ==0) && (ln.compare(pb[i].get_lastName(i)) ==0)){
    There is no advantage in using compare here. You can just write:
    Code:
    if (fn == pb[i].get_firstName(i) && ln == pb[i].get_lastName(i)) {
    Then, we come to what you were asking about:
    Code:
    pb[j+1].get_firstName(j+1).copy(pb[j].get_firstName(j), MAX_NAME_LENGTH);
    I am afraid that I cannot redeem this code. What you should do is provide a set_firstName function. Then you can write:
    Code:
    pb[j].set_firstName(j, pb[j + 1].get_firstName(j + 1));
    Actually, this whole business of first names and last names is probably wrong in itself. They should be bunded together in a single phone book entry object.
    iMalc likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to copy this string in int
    By fredsilvester93 in forum C++ Programming
    Replies: 5
    Last Post: 02-05-2012, 02:31 AM
  2. Replies: 3
    Last Post: 06-10-2011, 07:47 PM
  3. string copy
    By yangss in forum C Programming
    Replies: 4
    Last Post: 09-02-2010, 06:19 AM
  4. Replies: 1
    Last Post: 12-10-2008, 10:29 AM
  5. String copy
    By waxydock in forum C Programming
    Replies: 2
    Last Post: 06-02-2007, 08:39 PM

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