Reputation: 67
Hello Everyone I am trying to implement 2d array with of size 5*5 and some strings in it.When i=I am trying to print the 2d array in loops its giving error on "<<" sign,I dont know how to resolve it can anyone help me with it. Here is my code:
#include <iostream>
using namespace std;
int main()
{
string students[5][5]={"Wayne","Val","Yolanda","Zeus","Xavier",
"Yolanda","Wayne","Val","Xavier","Zeus",
"Wayne","Zeus","Xavier","Wayne","Zeus",
"Val","Yolanda","Xavier","Wayne","Zeus",
"Wayne","Yolanda","Val","Zeus","Xavier"};
for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
cout<<students[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Upvotes: 0
Views: 69
Reputation: 2041
You should include string
#include <string>
then:
#include <iostream>
#include <string> //<-------
using namespace std;
int main()
{
string students[5][5] = {
{"Wayne","Val","Yolanda","Zeus","Xavier"},
{"Yolanda","Wayne","Val","Xavier","Zeus"},
{"Wayne","Zeus","Xavier","Wayne" ,"Zeus"},
{"Val","Yolanda","Xavier","Wayne","Zeus"},
{"Wayne","Yolanda","Val","Zeus","Xavier"}
};
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
cout << students[i][j] << " ";
}
cout << endl;
}
return 0;
}
Upvotes: 3
Reputation: 3389
You are initializing a single array of length 25. You have to do the init like so:
string students[5][5]={ { "Wayne","Val","Yolanda","Zeus","Xavier" },
{ "Yolanda","Wayne","Val","Xavier","Zeus" },
{ "Wayne","Zeus","Xavier","Wayne","Zeus" },
{ "Val","Yolanda","Xavier","Wayne","Zeus" },
{ "Wayne","Yolanda","Val","Zeus","Xavier" } };
And, as François Andrieux mentionned in the comments, you should also include the proper header
#include <string>
because it defines the operator<<
for strings.
Upvotes: 1