-
August 15th, 2011, 09:54 AM
#1
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.
-
August 15th, 2011, 10:04 AM
#2
Re: Storing multiple array elements into one.
The question doesn't make sense. A single array element can only hold one value.
-
August 15th, 2011, 10:07 AM
#3
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]);
-
August 15th, 2011, 10:16 AM
#4
Re: Storing multiple array elements into one.
I'm going with Lindley's answer. As asked, the question doesn't make sense.
-
August 15th, 2011, 10:33 AM
#5
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.
-
August 15th, 2011, 10:51 AM
#6
Re: Storing multiple array elements into one.
Sounds like you may be looking for strcat.
-
August 15th, 2011, 10:57 AM
#7
Re: Storing multiple array elements into one.
-
August 15th, 2011, 11:12 AM
#8
Re: Storing multiple array elements into one.
Originally Posted by crosseyed
I don't like cats
That made me lol but GCDEF's suggestion is not a joke.
-
August 15th, 2011, 11:13 AM
#9
Re: Storing multiple array elements into one.
If you're trying to append characters to a string, just use std::string's += operator.
-
August 15th, 2011, 12:09 PM
#10
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
-
August 15th, 2011, 12:10 PM
#11
Re: Storing multiple array elements into one.
Last edited by crosseyed; August 17th, 2011 at 09:56 PM.
Reason: code tag
-
August 15th, 2011, 12:26 PM
#12
Re: Storing multiple array elements into one.
Code tags would go a long way to make that more readable.
-
August 15th, 2011, 12:29 PM
#13
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.
-
August 15th, 2011, 12:46 PM
#14
Re: Storing multiple array elements into one.
Originally Posted by Lindley
Code tags would go a long way to make that more readable.
How do you go about that?
-
August 15th, 2011, 12:48 PM
#15
Re: Storing multiple array elements into one.
Originally Posted by Lindley
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|