2d to 1d string copy!

sachin3858's Avatar, Join Date: Oct 2010
Newbie Member
hi!! i got stuck with this problem.som1 please help me out there!!
i have declared a global 2D char array namely char gnm[4][20].
and declared a 1D array in main()
namely nm[4];

In 1D array i am taking input from user in the following way
for(int i=0;i<5;i++)
{
cout<<"name";
cin>>nm;
gnm[i][]=nm // HERE I M STUCK I WANT TO KEEP THIS NAME IN MY 2D ARRAY.
}

WHAT SHOULD I DO........???
0
jimblumberg's Avatar
Ambitious contributor
First use code tags and more descriptive variable names.

Your var nm[4] should be set to the size of the name nm[20]; not the number of names expected.

You can not use the = operator on character arrays, you must use strcpy(). Or if you are just using nm[] for the input you could just
Code:
 cin >> gnm[i];
and avoid the copy.

I also suggest that you look into std::string to replace the character arrays.

Jim