Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   2d to 1d string copy! (http://www.go4expert.com/forums/2d-1d-string-copy-t23522/)

sachin3858 8Oct2010 19:20

2d to 1d string copy!
 
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........???

jimblumberg 8Oct2010 23:15

Re: 2d to 1d string copy!
 
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


All times are GMT +5.5. The time now is 05:32.