#include<iostream>
#include<string>
#include<conio.h>
using namespace std;
int main(){
    string array[5][5];
    int i=0,j=0;
    string z[5]={"abdg","ygur","guqh","asdf","sent"};
        for(i=0;i<5;i++)
         for(j=0;j<4;j++)
          { array[i][j]=z[i][j];
           cout<<"\narr[][]="<<array[i][j];}
     for(i=0;i<5;i++)
     cout<<"\narr="<<array[i]<<endl;      //this z not workin.WHY??...PLZ HELP!   

  getch();
  return 0;

    }

Recommended Answers

All 10 Replies

this isn't doing what i think you think it's doing. array[i] is an array of 5 strings, not an array of characters. There's no default way to insert arrays of things into a streaam, so the compiler will complain.

Here, this sould fix it:

#include<iostream>
#include<string>
using namespace std;
int main(){
    char array[5][5];
    int i=0,j=0;
    string z[5]={"abdg","ygur","guqh","asdf","sent"};
    for(i=0;i<5;i++)
        for(j=0;j<4;j++){
            array[i][j]=z[i][j];
            cout<<"\narr["<<i<<"]["<<j<<"]="<<array[i][j];
        }
    for (int i=0;i<5;i++)
        array[i][4]=0;//clears the buffer for unwanted characters.
    for(i=0;i<4;i++)
        cout<<"\narr="<<array[i]<<endl;
    return (0);
}

I changed the array type to char, as ravenous suggested, because you're accessing the string by their characters, so you'll need a character matrix, and after that I added a new for loop in which I clear the buffer for the elements of the array matrix.
Also don't use #include <conio.h> and getch() because they are really really bad.

commented: thanx a lot.... +0

Also don't use #include <conio.h> and getch() because they are really really bad.

Read bad as non-standard and prone to cause problems for you when you start using one of the 99% of the compilers that don't know what <conio.h> and getch() are.

The standard definition of bad doesn't apply. In rare cases, they are "really really good". But it's rare.

#include<iostream.h>
#include<string.h>
#include<conio.h>

int main()
{
     char array[5][5];
     int k=0;
     char z[20]="abdgygurguqhasdfsent";

     for(int i=0 ;i<5;i++)
     {
          for(int j=0 ;j<5;j++)
          {
                    array[i][j]=z[k];
                    k++;
          }
     }
      for(int a=0 ;a<5;a++)
     {
          for(int b=0 ;b<5;b++)
          {
             cout<<"\narr["<<a<<"]["<<b<<"]="<<array[a][b];
          }
    }
     return (0);

     getch();
}

I guess I should have added "and yours is not a rare case". Get rid of conio.h and getch()

Also, posting code with no information is bad. We like to know why we're looking at your code.

//sorry but i have wrongly written string array[5][5]....it should have been string //arr[5]....i m trying to store the  strings of array z into another array.....but i m //unable to get any o/p when the last o/p cout-line is executed.

#include<iostream>
#include<string>
using namespace std;
int main(){
string arr[5];
int i=0,j=0;
string z[5]={"abdg","ygur","guqh","asdf","sent"};;
for(i=0;i<5;i++)
    for(j=0;j<4;j++)
     {

array[i][j]=z[i][j]; //assign string char by char into arr
      cout<<"\narr[][]="<<arr[i][j]; //this line outputs the char being assigned....(1)

}
for(i=0;i<5;i++)
cout<<"\narr="<<array[i]<<endl;   //while line no (1) is working this doesnt gives any o/p.WHY SO??
//this z not workin. THERE IS NO ANY O/P. PLZZ HELP ME TO RESOLVE THIS PROBLEM!

  return 0;

}

If you want to put every char of an array of strings inside another string array you can do it like this:

#include <iostream>
#include <string>
using namespace std;
int main(){
    string arr[5];
    string str[5]={"aaaa", "bbbb", "cccc", "dddd", "eeee"};
    for (int i=0;i<5;i++){
        for (int j=0;j<4;j++)
            arr[i]+=(str[i][j]);//here arr[i] will "append" every character from the
                                //str array of strings.
    }
    for (int i=0;i<5;i++){
        cout<<arr[i]<<endl;
    }
    return (0);
}

i donot understand your question?
are u make the program in visual c or in turbo c++

Also, when you're using string arr[5] you have initialize an array of 5 strings, which are empty. So when you're trying to access an element, say [4][3] it would try to get the character located at [4][3] but would fail due to the fact that every string from that array of strings is empty, meaning "" and you can't access them as such. So, what to do then? Well initialize every string from the array with white spaces, like 4 characters of whitespace

#include<iostream>
#include<string>
using namespace std;
int main(){
    string arr[5];
    for (int i=0;i<5;i++)
        arr[i]="    "; //initialize your array so that you can access every character of it.
    string z[5]={"abdg","ygur","guqh","asdf","sent"};;
    for(int i=0;i<5;i++)
        for(int j=0;j<4;j++){
            arr[i][j]=z[i][j];
            cout<<"\narr[][]="<<arr[i][j];
        }
    cout<<endl;
    for (int i=0;i<5;i++)
        cout<<"Array: "<<arr[i]<<endl;
    return (0);
}

as you can see there.
There are multiple ways of doing that, another example is, if you don't want to initialize the array with white space, if you have arrays of different sizes, you can also use the push_back() function, that appends a character to the string, same as the push_back() from the stl vector for example.

int main(){
    string arr[5];
    string z[5]={"abdg","ygur","guqh","asdf","sent"};;
    for(int i=0;i<5;i++)
        for(int j=0;j<4;j++){
            arr[i].push_back(z[i][j]); //here you append the characters.
            cout<<"\narr[][]="<<arr[i][j];
        }
    cout<<endl;
    for (int i=0;i<5;i++)
        cout<<"Array: "<<arr[i]<<endl;
    return (0);
}

thanks a lot...lucaci andrew....this was of gr8 help....god bless u

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.