Quick Question!

This is a discussion on Quick Question! within the C++ Programming forums, part of the General Programming Boards category; ok how do i copy two variables let say char x[30] and char y[30] into char z[30] ?? Damn i ...

  1. #1
    Registered User dizz's Avatar
    Join Date
    Nov 2002
    Posts
    41

    Quick Question!

    ok how do i copy two variables let say

    char x[30]
    and
    char y[30]
    into
    char z[30]

    ??

    Damn i know this is easy but i cant find anything on this.
    Help is greatly appreciated!

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    do you want to add the two variables, or copy the stuff in memory into the new array...

    add two variables, like shown...

    z[30] = x[30] + y[30];

    if you want to copy them...you can't stick both in z[30]

    it would have to be something like;
    z[0] = x[30];
    z[1] = y[30];

    hope this helps...

  3. #3
    Registered User dizz's Avatar
    Join Date
    Nov 2002
    Posts
    41
    no not really
    i want to put two different strings into one variable

    pretty much

    x[30]="hello";

    y[30]="bob"

    and i want

    z[30]="hellobob";

    does anyone know how to do this

    i know it has something to do with strcpy but i have no clue!

  4. #4
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704

    hrmmm

    Well to do what you want to do, youd have to actaully make sure x and y combined are less then 30.

    It would be better if you found out how many characters were in x and y, and add that together to be the new Z[length].
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  5. #5
    Registered User dizz's Avatar
    Join Date
    Nov 2002
    Posts
    41
    okay aafter i do that what do i do?

  6. #6
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    If you use the <string> header you can just add the strings together.

    string h="hello";
    string w="world";
    string hw=h+w;
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  7. #7
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    or if you're looking to do it with the char arrays try sprintf() or strcat()
    hello, internet!

  8. #8
    Registered User dizz's Avatar
    Join Date
    Nov 2002
    Posts
    41
    no im not using arrays

    ok using rods method how would i then store string hw into a variable lets say name?

  9. #9
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    string hw - IS A VARIABLE no need to store it anywhere it's already stored.
    Code:
    string h="hello";
    string w="world";
    
    string hw = h + w; // This equals "helloworld"
    
    //Now watch this,
    hw=h+" "+w+" "+",did you know that this string variable can hold large strings and you can add them together with a \'+\' sign.";
    hw now equals:
    "hello world ,did you know that this string variable can hold large strings and you can add them together with a '+' sign."
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  10. #10
    Registered User dizz's Avatar
    Join Date
    Nov 2002
    Posts
    41
    okay i get all of this but now i want to put string hw into my variable vars.fullName for my structure
    how would i do that?

  11. #11
    Registered User dizz's Avatar
    Join Date
    Nov 2002
    Posts
    41
    ah hah i got it
    i was under the impression that u cannot declare strings in a function

    o well i figured it out!

  12. #12
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    Yes, it needs to be of the same type - your var must be a string or you'll be forced to do some TRIXXX to make it work.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  13. #13
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by dizz
    no im not using arrays

    ok using rods method how would i then store string hw into a variable lets say name?
    sure looked like you were a few posts ago

    you sure you have a handle on whatever it is you're asking?
    hello, internet!

  14. #14
    Registered User dizz's Avatar
    Join Date
    Nov 2002
    Posts
    41
    yeah thats cuz instead of using char arrays i just chose to use strings

  15. #15
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    x[30]="hello";

    y[30]="bob"

    and i want

    z[30]="hellobob";
    Code:
    strcat(z, x);
    strcat(x, y);
    Does this do the job.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

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