CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 4 1234 LastLast
Results 1 to 15 of 49
  1. #1
    Join Date
    Aug 2011
    Posts
    21

    Storing multiple array elements into one.

    How do I store multiple array elements into one?

    ex:
    Array [0]=Array1[3],Array7[3],Array4[2];

    I'm a newbie at this and any help is appreciated.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Storing multiple array elements into one.

    The question doesn't make sense. A single array element can only hold one value.

  3. #3
    Join Date
    Aug 2008
    Posts
    902

    Re: Storing multiple array elements into one.

    Uh, you could do it manually:

    Code:
    int myArray[3];
    myArray[0] = someArray1[3];
    myArray[1] = someOtherArray[3];
    myArray[2] = yetAnotherArray[2];
    But you are probably going to need a dynamic array, so you are better off using a vector.

    Code:
    vector<int> myVector;
    myVector.push_back(someArray1[3]);
    myVector.push_back(someOtherArray[3]);
    myVector.push_back(yetAnotherArray[2]);

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Storing multiple array elements into one.

    I'm going with Lindley's answer. As asked, the question doesn't make sense.

  5. #5
    Join Date
    Aug 2011
    Posts
    21

    Re: Storing multiple array elements into one.

    I have to generate random names.
    User inputs: # of names, # of letters in first name, and # of letters in last name.
    First letter (ea name) a capital consonant then alternate lowercase vowels and consonants.

    I created 3 functions for the above and have to store the resulting name in an array to be passed to a function and print out.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Storing multiple array elements into one.

    Sounds like you may be looking for strcat.

  7. #7
    Join Date
    Aug 2011
    Posts
    21

    Re: Storing multiple array elements into one.

    I don't like cats

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Storing multiple array elements into one.

    Quote Originally Posted by crosseyed
    I don't like cats
    That made me lol but GCDEF's suggestion is not a joke.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Storing multiple array elements into one.

    If you're trying to append characters to a string, just use std::string's += operator.

  10. #10
    Join Date
    Aug 2011
    Posts
    21

    Re: Storing multiple array elements into one.

    I've tried this two ways:
    Last edited by crosseyed; August 17th, 2011 at 09:55 PM. Reason: code tag

  11. #11
    Join Date
    Aug 2011
    Posts
    21

    Re: Storing multiple array elements into one.

    and....
    Last edited by crosseyed; August 17th, 2011 at 09:56 PM. Reason: code tag

  12. #12
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Storing multiple array elements into one.

    Code tags would go a long way to make that more readable.

  13. #13
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Storing multiple array elements into one.

    Okay. The first approach is clearly not good for two reasons. First, a different line of code for each possible numerical input is unsustainable beyond a small range of inputs. You need to determine a more compact way of expressing the idea.

    Second, the comma operator does not do what you think it does.

    Code:
    else if (i==1||3||5||7||9)
    This if statement does not check what you think it does either. Since 3 is not zero, this will always evaluate to true no matter what i is.

  14. #14
    Join Date
    Aug 2011
    Posts
    21

    Re: Storing multiple array elements into one.

    Quote Originally Posted by Lindley View Post
    Code tags would go a long way to make that more readable.
    How do you go about that?

  15. #15
    Join Date
    Aug 2011
    Posts
    21

    Re: Storing multiple array elements into one.

    Quote Originally Posted by Lindley View Post
    Okay. The first approach is clearly not good for two reasons. First, a different line of code for each possible numerical input is unsustainable beyond a small range of inputs. You need to determine a more compact way of expressing the idea.

    Second, the comma operator does not do what you think it does.

    Code:
    else if (i==1||3||5||7||9)
    This if statement does not check what you think it does either. Since 3 is not zero, this will always evaluate to true no matter what i is.

    the operator || means "or" correct?
    if i is equal to 1,3,5,7, or 9 is what my intent was.

Page 1 of 4 1234 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured