Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi I have a list of first names and I want to store them in an array. A string is an array of chars, so is an array of strings an array of array of chars?
i want to input data at runtime,
how can i do it?
int i;
char str[100][100];
for (i=0;i<100;i++)
cin.get(str[i],100);



i writed this code but it doesn't work
thank you
Posted

I think you want this instead (I may be wrong, my C++ foo has long gone unused):

C#
char* str[100];
for (int i = 0; i < 100; i++)
{
    cin >> str[i];
}
 
Share this answer
 
You asked the same question here (not same, but very similar):

input data into 2d char array[^]

Since you are using C++, the proper way to do this is using std::string. There's rarely a good reason to use char* based strings in C++ code.

Quick example:

C++
std::string str[100];
for (int i=0; i<100; i++)
{
        cin >> str[i];
}
 
Share this answer
 
v2
Comments
#realJSOP 20-Apr-11 15:02pm    
Not fair. :)

I had to get up from my char (yuk yuk) to talk to the boss. When I started my answer, there were no other answers posted.
Nish Nishant 20-Apr-11 15:06pm    
Sorry :-)

BTW you have stayed away from C++ too long. The code snippet you gave him will crash on the first iteration since the char*s have not been allocated! *grin*
#realJSOP 20-Apr-11 15:09pm    
Good! My work here is done. :)
Nish Nishant 20-Apr-11 15:11pm    
:-D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900