Apr 23, 2013 at 6:50am UTC
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 ===|
Apr 23, 2013 at 6:56am UTC
delete last comma in line {"3" ,"43" ,"34" ,"25" ,"33" },
Last edited on Apr 23, 2013 at 6:56am UTC
Apr 23, 2013 at 6:57am UTC
i still have the same error :(
Apr 23, 2013 at 7:04am UTC
/facepalm
It should be the other way round:
Either
string players[2][5]
or
= {{ "Marcus Banks" , "3" }, {"Linton Johnson" , "43" }, {/*....*/ }};
Apr 23, 2013 at 7:08am UTC
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 Apr 23, 2013 at 7:09am UTC
Apr 23, 2013 at 7:14am UTC
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 Apr 23, 2013 at 7:17am UTC
Apr 24, 2013 at 6:20am UTC
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;
}