help!

i got an error need help

#include <iostream>
using namespace std;


int main()
{
cout << "PLAYER NAME" << '\t' << "PLAYER NUMBER" << endl;
string players[5][2] = {{ "Marcus Banks","Linton Johnson","Paul Pierce","Earl Watson","Grant Hill"},
{"3","43","34","25","33"},
};

for(int z = 0; z<5; z++)
{
for (int j = 0; j<2; j++)
cout<< players[z][j] <<endl;
}

return 0;
}


Z:\C++ PROJECTS SUMMER\NBAPLAYERS2D.cpp||In function `int main()':|
Z:\C++ PROJECTS SUMMER\NBAPLAYERS2D.cpp|10|error: too many initializers for `std::string[2]'|
Z:\C++ PROJECTS SUMMER\NBAPLAYERS2D.cpp|10|error: too many initializers for `std::string[2]'|
||=== Build finished: 2 errors, 0 warnings ===|
delete last comma in line {"3","43","34","25","33"},
Last edited on
i still have the same error :(
/facepalm
It should be the other way round:
Either
string players[2][5]
or
= {{ "Marcus Banks", "3"}, {"Linton Johnson", "43"}, {/*....*/}};
i already tried this one = {{ "Marcus Banks", "3"}, {"Linton Johnson", "43"}, {/*....*/}};

the output should be this one

PLAYER NAME \t PLAYER NUMBER
Marcus Banks \t 3
Linton Johnson \t 43
Paul Pierce \t 34
Earl Watson \t 25
Grant Hill \t 33

also this one string players[2][5] i need 5 rows and 2 columns so this one should be string player[5][2].
Last edited on
Change your logic too. Because there is no other way. It is like if I wanted to name variable 5nake: not possible
What happens now:
1
2
3
4
5
6
string players[5][2] =                     //WTF?
{ /*Row 1 column 1*/ /*Row 1 Column 2*/ /*Row 1 Column 3*/ //...
{ "Marcus Banks",    "Linton Johnson",    "Paul Pierce",    "Earl Watson","Grant Hill"},
  /*Row 2 Column 1*/ //...
{       "3",           "43","34","25","33"},
};


Both ways works, however you should swap j and z if you swap indexes. Second way works out of box. And in your original code you didn't output tab character as you need...
Last edited on
well i actually got it now i should use this one
= {{ "Marcus Banks", "3"}, {"Linton Johnson", "43"}, {/*....*/}};

actually all i need was \t and \n -_-

#include <iostream>
using namespace std;
int main()
{
cout << "PLAYER NAME" << '\t' << "PLAYER NUMBER" << endl;
string players[5][2] = {{"Marcus Banks\t\t","3\n"},{"Linton Johnson\t\t","43\n"},
{"Paul Pierce\t\t","34\n"},{"Earl Watson\t\t","43\n"},
{"Grant Hill\t\t","33"}};
for (int j = 0; j < 5 ; j++)
{
for (int z = 0; z < 2 ; z++)
cout << players[j][z];
}
return 0;
}
Topic archived. No new replies allowed.