Thread: how to copy

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    479

    how to copy

    hi all,

    how do u copy a string to another string whithout using
    strcpy()

    sure if its possible use strcpy() but all i get is errors


    i'm using 2 for loops like this


    char temp[10];

    for(cin>>ansver;ansver!='q';cin>>ansver)

    for(int c=0;c<strlen(string);c++) //string is 10 slots
    {




    if(pointer[c]==answer) // pointer points to string

    {
    strcpy( temp[c], pointer[c]) // i thought something like this must
    do the job. but i get errors
    }} i know why i get errors but i dont know any other way to do this except memcpy() but that would'nt do
    the job

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Can you explain:
    Code:
    for(cin>>ansver;ansver!='q';cin>>ansver)
    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    Can you explain:

    code:--------------------------------------------------------------------------------
    for(cin>>ansver;ansver!='q';cin>>ansver)
    --------------------------------------------------------------------------------

    -Skipper





    yes it means


    as long as ansver is not equal the letter q
    ask for a new ansver


    u dont see it very often huh?

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    No, I don't. Why not a WHILE loop?

    (Not getting into that one again!)

    Your first FOR loop has no start brace. A typo?

    From my experience, (not that I've personally made this boo-boo, mind you! ), you might generate lots and lots of errors.

    Thinking out loud.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    Hello,

    Copying a string in C++ is very easy:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string a = "Hello, world!";
        string b;
    
        // copy a into b
        b = a;
    
        cout << b << '\n';
    }
    - lmov

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: how to copy

    Originally posted by pode
    hi all,

    how do u copy a string to another string whithout using
    strcpy()

    sure if its possible use strcpy() but all i get is errors


    i'm using 2 for loops like this


    char temp[10];

    for(cin>>ansver;ansver!='q';cin>>ansver)

    for(int c=0;c<strlen(string);c++) //string is 10 slots
    {




    if(pointer[c]==answer) // pointer points to string

    {
    strcpy( temp[c], pointer[c]) // i thought something like this must
    do the job. but i get errors
    }} i know why i get errors but i dont know any other way to do this except memcpy() but that would'nt do
    the job
    That doesn't make sense to me. Try this instead:
    Code:
    char String1[32] = "Hello world!";
    char String2[32] = "";
    
    strcpy(String2, String1);
    
    cout << String2;
    And if you're not allowed to use strcpy, make your own:
    Code:
    void strcpy(char* Buffer, char* String)
    {
       int Pos = 0;
       while(String[Pos] != '\0')
       {
          Buffer[Pos] = String[Pos];
          Pos++;
       }
       Buffer[Pos] = '\0';
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copy = concatenate ?
    By Arruba in forum C Programming
    Replies: 3
    Last Post: 11-03-2006, 04:54 PM
  2. calling copy constructor from template
    By Ancient Dragon in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2005, 01:54 PM
  3. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2005, 11:55 PM
  4. Copy constructors and operator=()
    By filler_bunny in forum C++ Programming
    Replies: 13
    Last Post: 08-25-2003, 07:43 AM
  5. Copy Constructor Help
    By Jubba in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 11:15 AM