Displaying in class functions

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

#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <iomanip>

using namespace std;

class player
	{
private:
	char Firstname[20];
	char Lastname[20];
	float FTA;//free throw attemps
	float FTM;//free throw made
	float FGA;//field goals attemps
	float FGM;//field goals made
	float FTP;//free percentage
	float FGP;//field goals percentage
	float R,A,S,B,G;//rebound, assist, steals, blocks, games
	float RPG;//rebound per game
	float PPG;//points per game
	float SPG;//steal per game
	float APG;//assist per game
	float BPG;//block shots per game


	void calgradeaverage(void);

public:
	void setData(float a, float b, float c, float d, float e, float f, float g, float h, float i); //public function members 
	player (float a = 1, float b = 1, float c = 1, float d = 1,float e = 1,float f = 1,float g = 1, float h = 1, float i = 1) {setData(a,b,c,d,e,f,g,h,i);}//default constructor 
	player(char name[]) {
    strcpy (Firstname, name);
  }
	
	//void setName (char name[]) {strcpy (Firstname, name);}
	//char getFirstname (void) {return Firstname[20];}
	//char getLastname (void) {return Lastname[20];}
	float getFTP() {return (FTM/FTA)*100;}
	float getFGP() {return (FGM/FGA)*100;}
	float getPPG() {return (FGM*2 + FTM)/G;}
	float getRPG() {return (R/G);}
	float getAPG() {return (A/G);}
	float getBPG() {return (B/G);}
	float getSPG() {return (S/G);}

	//void calcFTP(void){FTP = (FTM/FTA)*100;}//free throw percentage = free throw made over free throw attemps times 100
	//void calcFGP(void){FGP = (FGM/FGA)*100;}//field goal percentage = field goal made over field goal attemps times 100
	//void calcPPG(void){PPG = (FGM*2 + FTM);}//points per game = field goal x 2 + free throw x 1

	//void calcRPG(void){RPG = (R / G);}//rebound per game
	//void calcSPG(void){SPG = (S / G);}//steal per game
	//void calcAPG(void){APG = (A / G);}//assist per game
	//void calcBPG(void){BPG = (B / G);}//blocks shots per game




	~player(void) {cout<<"bye bye!\n";}
};


void player::setData(float a, float b, float c, float d, float e, float f, float g, float h, float i)
{

	FTA = ((a>=0) ? a : 0);
	FTM = ((b>=0) ? b : 0);
	FGA = ((c>=0) ? c : 0);
	FGM = ((d>=0) ? d :0);
	R = ((e>=0) ? e :0);
	A = ((f>=0) ? f :0);
	S = ((g>=0) ? g :0);
	B= ((h>=0) ? h :0);
	G = ((i>=0) ? i :0);
	
};




int main()
{

	float a, b, c, d,e,f,g,h,i;
	char fname;
	char lname;

	cout<<"please enter FTA ";
	cin>>a;
	cout<<"please enter FTM ";
	cin>>b;
	cout<<"please enter FGA ";
	cin>>c;
	cout<<"please enter FGM ";
	cin>>d;
	cout<<"please enter rebounce ";
	cin>>e;
	cout<<"please enter assist ";
	cin>>f;
	cout<<"please enter staels ";
	cin>>g;
	cout<<"please enter blocks ";
	cin>>h;
	cout<<"please enter how many games he played";
	cin>>i;
	cout<<endl;
	player keith;
	keith.setData(a,b,c,d,e,f,g,h,i);
	cout<<fixed<<setprecision(1);	
	cout<<"the player's free throw percentage is"<<keith.getFTP()<<"%"<<endl;
	cout<<"the player's field goal percentage is"<<keith.getFGP()<<"%"<<endl;
	cout<<"the player's points per game is "<<keith.getPPG()<<endl;
	cout<<"the player assist per game is " <<keith.getAPG()<<endl;
	cout<<"the player's steals per game is" <<keith.getSPG()<<endl;
	cout<<"the player's blocked shots per game is" <<keith.getBPG()<<endl;
	


	cin.get();
}

//steals per game, and blocked shots per game.  The two shooting percentages are computed by dividing shots made by shots attempted and multiplying by 100.  The per game averages are computed by dividing the raw statistic 


Well as you can see i am trying to do a input ur name and display ur name code. But it just doesn't display it.

the requirements:
1) you MUST use an array name so char firstname[20] is a must.
2) u need to use a class public and private

Last edited on
The first error I see right away is on line 20. You're spelling "your" wrong.
1
2
3
char getFname (void) { //why are you returning just 1 character ?
  return firstname[20]; //out of bounds
}

Don't use setters, use the constructor.
player keith(fname);
it gave me an error if i made a constructor like that.
You have to define the constructor.
1
2
3
4
5
6
7
8
9
class player {
  ...
 public:
  //constructor
  player(char name[]) {
    strcpy(firstname, name);
  }
  ...
};
Last edited on
okay, becuase i am not fimliar with how to use contrsuctor to do inputs what am i suppose to store it in main?

cout<<"what's ur name"
cin>>keith.player//that sounds so wrong lol

i post my complete code up too check it out it's actually a NBA data code. the only thing i am missing is how to input anema nd display name.
Last edited on
You are doing it right. The problem is in the getFname function.
1
2
// char getFname (void) {return firstname[20];} //here you are returning a single (out-of-bounds) char
char[] getFname() const { return firstname; } //you should be returning the whole c-string 
Last edited on
^Also if you want to do it that way you should probably have the function be const and return a const char* instead of just a single character (which would give you an error anyway since firstname is a char*).
Haha whoops!
Last edited on
firedraco can u repeat it again? what do u mean? when u did char* did u mean pointers? and i am nto allow to use pointers in this. (becuase we just learn how to use pointers to do it but this project is before we learn it so we are not allow to use it.)
You can't learn about arrays without learning about pointers.
mathhead200 what do u mean i am doing it right?

i have a constructor and a get function?
a constructor of
char getFname (void) { //why are you returning just 1 character ?
return firstname[20]; //out of bounds
}
and a get function of
char[] getFname() const { return firstname; }?

then u say my statements will be
cout<<"what's ur name"
cin>>keith.player; ? because that's not right or i am not getting something/.
No! You still use the getFname function.
You just need to define it so it returns the whole name, instead of one (out-of-bound) char.
char[] getFname() const { return firstname; } //you should be returning the whole c-string
Topic archived. No new replies allowed.