English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

i want to store 3 names say john,mac and henry in an array in c++
what code should i write in c++ so as to facilitate this coz i tried doing this
char name[ ]={"john","Mac","Henry"};
this is not working y?????????????
can you plz suggest some possible way to as to get names stored in a array

2006-09-07 04:44:17 · 7 answers · asked by dudezhere_7889 1 in Computers & Internet Programming & Design

7 answers

You do not need to use string, but if it is easier, you can.

You get an error at the moment, is because "john" it self would be an array of chars. You are trying to create an array of "arrays of chars"

Convert your statement above to
char[3][] = {"john" ... }
You need to add a number somewhere there, whether it be the 3, as in how many names you have, or
char[][6] = {"john" ... }
if you need to know how many chars in the second array. Henry is longest name at 5 chars, you need to add 1 char for a "null character" to end the name.

This is called a multiple array.

2006-09-09 09:03:38 · answer #1 · answered by Mark aka jack573 7 · 0 0

Yes, use strings because otherwise you are dealing with pointers which is something I guess you dont want to do. Here is a possible solution that I just did that you could use if you were using character arrays instead of strings. Bear in mind that c++ is not my strong point so I'm open to correction on this.

#include
#include

using namespace std;

int main(int argc, char *argv[])
{
char **names = new char*[2];
names[0] = "jack";
names[1] = "jill";

std::cout << "Name 1: " << names[0] << endl;
std::cout << "Name 2: " << names[1] << endl;

system("pause");
return EXIT_SUCCESS;
}

2006-09-07 05:06:31 · answer #2 · answered by Anonymous · 0 0

Either string.h or char arrays, because C++ don't have string as standard types, try char *name[] instead. in your code I mean.

2006-09-07 05:09:57 · answer #3 · answered by Andy T 7 · 0 0

Does C++ use strings?If so, shouldnt those be declared as strings and not as chars?

2006-09-07 04:50:41 · answer #4 · answered by rcrespo@sbcglobal.net 2 · 0 1

#include

using namespace std;

void main()
{
string name[]={"john","Mac","Henry"};

2006-09-07 04:58:56 · answer #5 · answered by GNR Sam 3 · 0 0

char* name[3]={"John","Mac","Henry"};

2006-09-09 12:41:41 · answer #6 · answered by DukeInstinct 2 · 0 0

#include
#include
void main()
{
char name[]={"john","mac","henry"};
....
...
}
try this way

2006-09-07 05:06:50 · answer #7 · answered by george 4 · 0 0

fedest.com, questions and answers