• Advertisement
Sign in to follow this  

String Arrays.

This topic is 4262 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can someone point me in the right direction?? Im trying to put some names in an array! myArray[3] myArray[0] = "John" myArray[1] = "Joe" myArray[2] = "Jose" myArray[3] = "Jill" printf("%s", myArray[2]) output: Jose press any key to continue... ********* (whatever! you know) Ive been looking around but cant find anything on it im gonna keep looking though. thx

Share this post


Link to post
Share on other sites
Advertisement
Which programming language are you using?

Share this post


Link to post
Share on other sites
The C++ equivalent of the (presumably pseudo-) code that you posted might look something like this:
std::vector<std::string> myArray(4);

myArray[0] = "John";
myArray[1] = "Joe";
myArray[2] = "Jose";
myArray[3] = "Jill";

std::cout << myArray[2] << std::endl;
If any of this looks unfamiliar, just ask.

Share this post


Link to post
Share on other sites
I am just guessing here, but won't this work?

char myArray[3][20];
myArray[0][]="Name1\0";
myArray[1][]="Name2\0";

Something like that?

Share this post


Link to post
Share on other sites
Quote:
Original post by wildex999
I am just guessing here, but won't this work?

char myArray[3][20];
myArray[0][]="Name1\0";
myArray[1][]="Name2\0";

Something like that?
Perhaps you mean something like this:
char* myArray[2];
myArray[0] = "Name1";
myArray[1] = "Name2";
In any case, this is more idiomatic for C, whereas the OP specified C++.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bluseed
hmmm 4 some reason those dont work for me?
Post the code that doesn't work, along with whatever errors you're getting.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bluseed
hmmm 4 some reason those dont work for me?

If you use the vector of strings, make sure to include the corresponding headers, i.e.


#include <vector> // provide std::vector
#include <string> // provides std::string
#include <iostream> // gives you input/output streams

int main() {
std::vector<std::string> array(4);
array[0] = "one";
array[1] = "two";
array[2] = "three";
array[3] = "four";
// vector allows you to add some more elements like this:
array.push_back( "five" );

// this should print "five" now.
std::cout << array[4] << "\n";
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement