string copy question

This is a discussion on string copy question within the C++ Programming forums, part of the General Programming Boards category; hi there , i am writing a prg which is the same as the library strcpy function .. could u ...

  1. #1
    san
    san is offline
    Registered User
    Join Date
    Jul 2002
    Posts
    13

    string copy question

    hi there , i am writing a prg which is the same as the library strcpy function ..

    could u guys check out my prg because it is copying b to a but at the end it is giving me some other char ..

    //************************************************** *
    #include<iomanip.h>
    #include<string>
    //************************************************** *
    //prototypes
    void myStrcpy(char a[], char b[]);
    //************************************************** *
    const int MAX = 10;

    //************************************************** *
    void main()
    { char a[MAX],
    b[MAX];

    myStrcpy(a, b);

    }
    //************************************************** *
    void myStrcpy(char a[], char b[])
    {
    int num,
    ct;

    cout << "Enter the word" ;
    for(ct = 0 ; ct <= 5 ; ++ct){
    cin >>ws;
    cin>> b[ct]; }

    do{
    for(ct = 0 ; b[ct]!='\0' ; ++ct){
    a[ct]= b[ct] ;}
    }while(b[ct]!= '\0');
    cout << "b is copied to a and now a is" << a << endl;
    }

  2. #2
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    From a quick glance at your code, it seems you are not copying the NULL termination character. You need to copy this, and them terminate.

    Alternatively, you could use memcpy to copy the string contents, and set the byte after the last character copied to be NULL.

  3. #3
    san
    san is offline
    Registered User
    Join Date
    Jul 2002
    Posts
    13
    i am nwe to this so please could u show me hoe to do that.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    79
    #include<string.h>

    char *mystrcpy(char *ptr1,char *ptr2)
    {
    int *i; i=new int;
    for(*i=0; *i<strlen(ptr2); (*i)++)
    {
    ptr1[*i]=ptr2[*i];
    }
    while(*i<strlen(ptr1))
    {
    ptr1[*i]=0;
    (*i)++;
    }
    delete i;
    return(ptr1);
    }

    Compiler:Borland C++ 5.0
    Last edited by sundeeptuteja; 07-15-2002 at 12:25 PM.

  5. #5
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    Code:
    void myStrcpy(char a[], char b[])
    {
        int     num, ct;
        cout << "Enter the word" ;
        for(ct = 0 ; ct <= 5 ; ++ct){
            cin >>ws;
            cin>> b[ct];
        }
        // Why not:
        cin.getline(b,MAX-1);
        
        do{ //This loop is useless
            for(ct = 0 ; b[ct]!='\0' ; ++ct){
                a[ct]= b[ct] ;
            }
        }while(b[ct]!= '\0'); // You should set '\0' to the end of a.
        cout << "b is copied to a and now a is" << a  << endl;
    }

  6. #6
    san
    san is offline
    Registered User
    Join Date
    Jul 2002
    Posts
    13
    OK WHEN I USE cin.getline it si giving me the following error

    Error E2285 C:\srt_cpy.cpp 29: Could not find a match for 'istream::getline(char,int)' in function myStrcpy(char *,char *)

    and i removed the do while loop and just used while

    while(b[ct]!= '\0')---> this is giving the copied value

    BUT
    when i used this one

    while(a[ct]!= '\0') ---> this is giving some garbage values

    i dont know what i am doing wrong..

  7. #7
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    OK WHEN I USE cin.getline it si giving me the following error
    Error E2285 C:\srt_cpy.cpp 29: Could not find a match for 'istream::getline(char,int)' in function myStrcpy(char *,char *)
    You seem to be using it like getline(b[index],N).
    But you don't need the first for loop at all.
    Only the getline in my previous post.

    and i removed the do while loop and just used while
    while(b[ct]!= '\0')---> this is giving the copied value
    Code:
    [edit]fixed[/edit]
    while(ct=0;b[ct]!='\0' ; ct++){ 
        a[ct]= b[ct] ;
    }
    a[ct]='\0';
    Last edited by raimo; 07-15-2002 at 10:37 AM.

  8. #8
    san
    san is offline
    Registered User
    Join Date
    Jul 2002
    Posts
    13
    cin.getline(b,MAX-1);

    could u explain to me why did u use MAX-1 [ can i say
    cin.getline(b,sizeof(b)) instead of cin.getline(b,MAX-1); ]

    a[ct]='\0';
    if i assign a[ct] the null char what about the contents it copied from b ..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. function to copy one string to another one
    By robstr12 in forum C Programming
    Replies: 15
    Last Post: 01-28-2005, 10:46 PM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM

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