1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

2d to 1d string copy!

Discussion in 'C++' started by sachin3858, Oct 8, 2010.

  1. sachin3858

    sachin3858 New Member

    Joined:
    Oct 8, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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[]=nm // HERE I M STUCK I WANT TO KEEP THIS NAME IN MY 2D ARRAY.:(
    }

    WHAT SHOULD I DO........???
     
  2. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    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
     

Share This Page