How to display cstrings?

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


#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];}
       //char[] getFname() const { return Firstname; }
	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);}





	~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 


as you can see i got everythign done other than displaying the cstring for the player's first and last name (i am not fimliar on how to do it) can anyone help me out with this? (you can also i tried differnt attempts on doing it too as i comment it out in the public:
Last edited on
Actually this is my 5th post ont eh same question i just want to know how to make hte user input a name and cout what the user input by using class private/public set get w/e
example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cstring>

struct Player{
   char name[20];
   
   void set( char[] n ){
       strcpy(name, n);
   }
   char* get(){
      return name;
   }
};

int main(){
   Player Bob;
   char temp[20];//you don't really need this (or Player::set)
   std::cin.getline(temp, 20);//cin >> would ignore the length of temp and thus be dangerous.
   Bob.set(temp);
   std::cout << Bob.get();
   return 0;
}


Though it might be smarter to use std::string instead.
Last edited on
1>d:\spring 2010\cmpsc\cmpsc 122\cpp2_1111\cpp2_1111\cpp2_1.cpp(7): error C2146: syntax error : missing ')' before identifier 'n'
1>d:\spring 2010\cmpsc\cmpsc 122\cpp2_1111\cpp2_1111\cpp2_1.cpp(7): error C3646: 'n' : unknown override specifier
1>d:\spring 2010\cmpsc\cmpsc 122\cpp2_1111\cpp2_1111\cpp2_1.cpp(7): error C2059: syntax error : ')'
1>d:\spring 2010\cmpsc\cmpsc 122\cpp2_1111\cpp2_1111\cpp2_1.cpp(8): error C2065: 'n' : undeclared identifier
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
Last edited on
Sorry.
It should either be void set(char* n){ or void set(char n[]){
@hunkeenlin : i think you should consults this code !
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
#include <iostream>
#include <string>
#include <sstream>


using namespace std;

class player
	{
private:
	char Firstname[20];
	char Lastname[20];
     float R;
	float G;

public:
	void getFirstname () 
	{
		cout<< " enter first name : ";
		cin.getline(Firstname,20);
		fflush(stdin);
		cout<<" Enter player rebounce : ";
		cin>>R;
		cout<<" Enter player the played : ";
		cin>>G;
     }

	float getRPG () 
	{  
		return (R/G);
	}
};
	
int main()
{
	player keith;
        keith.getFirstname();
	cout<<"the rebounce per game for p[layer keith is: "<<keith.getRPG()<<endl;
	system ("pause");
	
}
Topic archived. No new replies allowed.