0

Hello all I am trying to make a code for a game that I want to make as of right now I am kinda stuck on classes and wanted use the list initialization to make it easier for to enter the players attributes here is my class below this is part of my main code as I did not want to make a header or another .cpp file yet.

class Player {

public:

string name;
int id;
int height;
double closeshot;
double midshot;
double threeshot;
double dribble;
double defense;
double jump;

Player() {};
Player(string a, int b, int c, double d, double e, double f, double g, double h, double i);


}pg1,pg2,sg1,sg2;


Player::Player(string a, int b, int c, double d, double e, double f, double g, double h, double i) {

    name = a;
    id = b;
    height = c;
    closeshot = d;
    midshot = e;
    threeshot = f;
    dribble = g;
    defense = h;
    jump = i;

}

Here is my player initiliaztion list below.

Player pg1( "Player1Home", 1, 61, 10.00, 10.00, 10.00, 10.00, 10.00, 10.00 );
Player pg2( "Player1Away", 6, 72, 10.00, 10.00, 10.00, 10.00, 10.00, 15.00 );
Player sg1( "Player2Home", 2, 61, 10.00, 10.00, 10.00, 10.00, 10.00, 10.00 );
Player sg2( "Player2Away", 7, 71, 10.00, 10.00, 10.00, 10.00, 10.00, 10.00 );

Any help would be appreciate im still relatively new at this.

So I decided to just go back to structs as they are just easier to work with

struct Player {

string name;
int id;
int height;
double closeshot;
double midshot;
double threeshot;
double dribble;
double defense;
double jump;


}pg1,pg2,sg1,sg2;

And this is how I added in my values

        pg1.name = "Player1Home";
        pg1.id = 1;
        pg1.height = 61;
        pg1.closeshot = 10.00;
        pg1.midshot = 10.00;
        pg1.threeshot = 10.00;
        pg1.dribble = 10.00;
        pg1.defense = 10.00;
        pg1.jump = 10.00;

        pg2.name = "Player1Away";
        pg2.id = 6;
        pg2.height = 61;
        pg2.closeshot = 10.00;
        pg2.midshot = 10.00;
        pg2.threeshot = 10.00;
        pg2.dribble = 10.00;
        pg2.defense = 10.00;
        pg2.jump = 10.00;

        sg1.name = "Player2Home";
        sg1.id = 2;
        sg1.height = 61;
        sg1.closeshot = 10.00;
        sg1.midshot = 10.00;
        sg1.threeshot = 10.00;
        sg1.dribble = 10.00;
        sg1.defense = 10.00;
        sg1.jump = 10.00;

        sg2.name = "Player2Away";
        sg2.id = 7;
        sg2.height = 61;
        sg2.closeshot = 10.00;
        sg2.midshot = 10.00;
        sg2.threeshot = 10.00;
        sg2.dribble = 10.00;
        sg2.defense = 10.00;
        sg2.jump = 10.00;

The rest of my code just works better when I leave it this way.

  • One of the issues that I am running into for this code is that when I actually run it the data isn't actually being picked up for example say I were to use pg1.name there is no output showing up on my code. – rainguy826 Dec 02 '17 at 23:00
  • 1
    "*[...] I am kinda stuck on classes and wanted use the list initialization to make it easier for to enter the players attributes *" what do you *easier to enter*? and what's the problem of using it here? If you don't know how to do so, please read a good book or watch a tutorial – Fureeish Dec 02 '17 at 23:00
  • Basically I dont want have to keep entering pg1.id = "blah blah" p1.name = "blah blah" if I can use list initialization. – rainguy826 Dec 02 '17 at 23:01
  • Your initialization you are creating local variables with the same name as your globals. These will be different variables. With that said I would not use globals. – drescherjm Dec 02 '17 at 23:02
  • So I should keep the variables private? – rainguy826 Dec 02 '17 at 23:03
  • They should exist in whatever scope that needs them. You have not showed what classes interact with Player and where you need your players created. Usage of global variables should be avoided if possible. – drescherjm Dec 02 '17 at 23:05
  • Would you recommend using a combination of get and sets for the constructers to pull out the data for my code? – rainguy826 Dec 02 '17 at 23:29
  • 1
    I would recommend not declaring `pg1,pg2,sg1,sg2` after `Player`'s declaration – Fureeish Dec 02 '17 at 23:32
  • I reverted my code back to what worked – rainguy826 Dec 03 '17 at 00:10
  • [We don't recommend getters and setters in general](https://stackoverflow.com/questions/565095/are-getters-and-setters-poor-design-contradictory-advice-seen), except when they are *really* needed. Which is a special case. – Bo Persson Dec 03 '17 at 00:13
  • I am not sure you understand the reason why `Player pg1( "Player1Home", 1, 61, 10.00, 10.00, 10.00, 10.00, 10.00, 10.00 );` failed. Here you are creating a new variable `pg1` that has no relation to the `pg1` you declared after Player. This is why you set its value only to see it disappear. – drescherjm Dec 03 '17 at 01:06
  • Also I agree with @Fureeish. Generally c++ programmers do not declare our variables `pg1,pg2,sg1,sg2` the way you did after the class. – drescherjm Dec 03 '17 at 01:08
  • How is it suppose to look @drescherjm do you have any example I'm following guides – rainguy826 Dec 03 '17 at 02:07
  • Normally you would have your players in some class, struct or other container. – drescherjm Dec 03 '17 at 02:31

0 Answers0