issue with methods

there is something wrong with my methods please help

#include <iostream>
#include <iomanip>

using namespace std;

//*************** Place your class description after this line ***************

class Player
{
public:
Player();
Player( const char [], int, int, int );

void printPlayer();

void setName(const char [] );
void setGoals( int );
void setAssists( int );
void setRating( int );

int getGoals();
int getAssists();
int getRating();

private:
char name[50];
int goals;
int assists;
int rating;
};

//****************************************************************************

int main()
{
//Test 1 -- default constructor
Player player1=Player( "Kushtrim Berisha/Greg Johnsen", 1, 1, 1);
Player player2=Player();
Player player3=Player("Johanthan Toews", 10, 9, 6);
Player player4=Player("Patrick Kane", 11, 10, -3);
Player player5=Player("Brandon Saad", 5,8, 8);
Player player6=Player("Brandon Pirri",6,5,6);

cout<<"First Player Object"<<endl;
player1.printPlayer();
cout<<endl<<endl;
/*
player1.setGoals(2);
player1.printPlayer();
cout<<endl<<endl<<endl;

cout<<"Second Player Object"<<endl;
player2.printPlayer();
cout<<endl;endl;

player2.setName("Patrick Sharp");
player2.setGoals(7);
player2.setAssists(13);
player2.setRating(10);
player2.printPlayer();
cout<<endl;endl;

//**************************************************************************************
cout<<"Third Player Object"<<endl;
player3.printPlayer();
cout<<endl;

player3.setGoals(-8);
player3.setAssists(-2);
player3.printPlayer();
cout<<endl;endl;

//**************************************************************************************
cout<<"Fourth Player Object"<<endl;
player4.printPlayer();
cout<<endl<<endl;

player4.setAssists(3);
player4.setRating(3);
player4.printPlayer();
cout<<endl<<endl;

//**************************************************************************************
cout<<"Fifth Player Object"<<endl;
//cout only goals and plus minus?????????????????????????????????????????????????????????

player5.printPlayer();
cout<<endl<<endl;

//**************************************************************************************
cout<<"Sixth Player Object"<<endl;
player6.printPlayer();
cout<<endl;

player6.setAssists(-2);
player6.setAssists(4);
//player6. see out only assists??????????????????????????????????????????????????????????
player6.setRating(-4);
player6.printPlayer();
cout<<endl<<endl;


*/





//Test 2 -- constructor with arguments



// Setting Names, Goals, Assists and Rating


// getGoals, getAssists and getRating

//methods

Player::Player()
{
char setName('\0');

int setGoals = 0;
int setAssists = 0;
int setRatings = 0;
}


//******************************************************************************************************
Player::Player (const char[n], int g, int a, int r)
{
strcpy (playerName, char[n]);
setGoals(g);
setAssists(a);
setRatings(r);
}


//******************************************************************************************************
void Player::printPlayer()
{
cout<<""<<name<<endl;
int points;
points = goals + assists;
cout<<"Goals: "<<goals<<" Assists: "<<assists<<" Points: "<<points<<" Plus/Minus: "<<rating;
}

//******************************************************************************************************
void Player::setName(const char playerName[i])
{
int i;
while ( char [i] != 0)
{
playerName += name[i];
i++:
}
cout<<""<<name[i];
}

//******************************************************************************************************
void Player::setGoals (int goalsScored)
{
if (goalsScored >= 0)
goals+=goalsScored;

else
goals +=0;
cout<< "setGoals error: goals scored cannot be negative";
}

//******************************************************************************************************
void Player::setAssists(int assistsEarned)
{
if (assistsEarned >=0)
assists+=assistsEarned;

else
assists+=0;
cout<< "setAssist error: assists earned cannot be negative";
}
//******************************************************************************************************

void Player::setRating(int plusMinusChange)
{
if (plusMinusChange >= 0)
rating+=plusMinusChange;

else
rating+=0;
}
//*****************************************************************************************************

int Player::getGoals()
{
goalsScored= goals +goalsScored;
return goalsScored;
}

int Player::getAssists()
{
assistEarned = assists + assistsEarned;
return assistsEarned;
}

int Player::getRating()
{
plusMinusChange = rating + plusMinusChange;
return plusMinusChange;
}


There are so many errors in your code, so I am formatting this code to make it easier to read.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//*************** Place your class description after this line ***************

class Player
{
public:
Player();
Player( const char [], int, int, int );

void printPlayer();

void setName(const char [] );
void setGoals( int );
void setAssists( int );
void setRating( int );

int getGoals();
int getAssists();
int getRating();

private:
char name[50];
int goals;
int assists;
int rating;
};

//****************************************************************************

int main()
{
//Test 1 -- default constructor
Player player1=Player( "Kushtrim Berisha/Greg Johnsen", 1, 1, 1);
Player player2=Player();
Player player3=Player("Johanthan Toews", 10, 9, 6);
Player player4=Player("Patrick Kane", 11, 10, -3);
Player player5=Player("Brandon Saad", 5,8, 8);
Player player6=Player("Brandon Pirri",6,5,6);

cout<<"First Player Object"<<endl;
player1.printPlayer();
cout<<endl<<endl;
/*
player1.setGoals(2);
player1.printPlayer();
cout<<endl<<endl<<endl;

cout<<"Second Player Object"<<endl;
player2.printPlayer();
cout<<endl;endl;

player2.setName("Patrick Sharp");
player2.setGoals(7);
player2.setAssists(13);
player2.setRating(10);
player2.printPlayer();
cout<<endl;endl;

//**************************************************************************************
cout<<"Third Player Object"<<endl;
player3.printPlayer();
cout<<endl;

player3.setGoals(-8);
player3.setAssists(-2);
player3.printPlayer();
cout<<endl;endl;

//**************************************************************************************
cout<<"Fourth Player Object"<<endl;
player4.printPlayer();
cout<<endl<<endl;

player4.setAssists(3);
player4.setRating(3);
player4.printPlayer();
cout<<endl<<endl;

//**************************************************************************************
cout<<"Fifth Player Object"<<endl;
//cout only goals and plus minus?????????????????????????????????????????????????????????

player5.printPlayer();
cout<<endl<<endl;

//**************************************************************************************
cout<<"Sixth Player Object"<<endl;
player6.printPlayer();
cout<<endl;

player6.setAssists(-2);
player6.setAssists(4);
//player6. see out only assists??????????????????????????????????????????????????????????
player6.setRating(-4);
player6.printPlayer();
cout<<endl<<endl;


*/





//Test 2 -- constructor with arguments



// Setting Names, Goals, Assists and Rating


// getGoals, getAssists and getRating

//methods

Player::Player()
{
char setName('\0');

int setGoals = 0;
int setAssists = 0;
int setRatings = 0;
}


//******************************************************************************************************
Player::Player (const char[n], int g, int a, int r)
{
strcpy (playerName, char[n]);
setGoals(g);
setAssists(a);
setRatings(r);
}


//******************************************************************************************************
void Player::printPlayer()
{
cout<<""<<name<<endl;
int points;
points = goals + assists;
cout<<"Goals: "<<goals<<" Assists: "<<assists<<" Points: "<<points<<" Plus/Minus: "<<rating;
}

//******************************************************************************************************
void Player::setName(const char playerName[i])
{
int i;
while ( char [i] != 0)
{
playerName += name[i];
i++:
}
cout<<""<<name[i];
}

//******************************************************************************************************
void Player::setGoals (int goalsScored)
{
if (goalsScored >= 0)
goals+=goalsScored;

else
goals +=0;
cout<< "setGoals error: goals scored cannot be negative";
}

//******************************************************************************************************
void Player::setAssists(int assistsEarned) 
{
if (assistsEarned >=0)
assists+=assistsEarned;

else
assists+=0;
cout<< "setAssist error: assists earned cannot be negative";
}
//******************************************************************************************************

void Player::setRating(int plusMinusChange)
{
if (plusMinusChange >= 0)
rating+=plusMinusChange;

else
rating+=0;
}
//*****************************************************************************************************

int Player::getGoals()
{
goalsScored= goals +goalsScored;
return goalsScored;
}

int Player::getAssists()
{
assistEarned = assists + assistsEarned;
return assistsEarned;
}

int Player::getRating()
{
plusMinusChange = rating + plusMinusChange;
return plusMinusChange;
}


Line 21: You should use a std::string object instead of a character array. (Also, indent your code, please)

Line 32: The statement Player player1=Player( "Kushtrim Berisha/Greg Johnsen", 1, 1, 1); is redundant. Type Player player1( "Kushtrim Berisha/Greg Johnsen", 1, 1, 1); instead.
NOTE: Write Line 33 like this: Player player2;

Line 41: You can't use std::endl without first using std::cout. Also, don't use it more than twice. Instead, use it once, and print out "\n" the desired amount of lines you wish to skip.

Line 42: I don't know whether you commenting this entire section is intentional or not, but I just felt like I had to point it out.

Line 117: You're declaring a bunch of new variables. Instead, you should set the values of the variables in your class.
1
2
3
4
name = "\0";
goals = 0;
assists = 0;
ratings = 0;


Line 126: Your not defining a name for your const char[]. Also, that's not the way of getting the size of an array. The const char[] is automatically promoted to a pointer, which means it does not store the size of the array. For simplicity's sake, I recommend using a std::string, as it allows you to get the size of the array using the size() member function.

Line 128: You can't use char[] to access an array. You have to give the variable a name, and then refer to it through that. Also, the variable n has never been defined. And finally, I don't believe you ever included the necessary header for that function. Try to include <string.h>

Line 138: The first character string there is completely unnecessary. Get rid of it.

Line 140: There's no point to set the value at another statement. Assign the value as the variable is declared.
int points = goals + assists;

Line 141: You never placed a std::endl at the end of this.

Line 145: Same problem as on line 126. Also, a setter function should typically only set a member variable to another variable.

Line 148: There is no variable called char. Also, char is a reserved keyword, and can't be used to define the name of a variable.

Line 150: Your trying to add an array with a char. This is impossible. Also, your statements seem to be reversed. Your trying to set the value of the parameter which was passed in.

Line 151: A while loop is redundant here. Use a for-loop instead.

Line 154: Use a std::string and rewrite the function. You can set one std::string to another std::string without any problems.

Line 163: Get rid of this statement. Also, encase your if's and else's with a block. In order words, encase those with {...} brackets. This applies to all of your setters.

Line 197: There is a clear spelling error here. It should be assistsEarned, not assistEarned. Also, your code is kind of redundant. Write this instead:
return assistsEarned += assists;

The previous redundancy fix should apply to all your getters.

That are all the errors I can see.
Topic archived. No new replies allowed.