help please!!

can someone help me with this. i have created a player class to display player names and points. points need to add up when the games go on. at the beginning points should be displayed as 0. this is the output i get

Welcome to Pairs
Select Menu Option
1. Game Rules
2. Play Game
2
How many players in the game?
2
Enter name:
ben
Enter name:
tom



ben-858993460
tom-858993460


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef PLAYER_H
#define PLAYER_H

#include<string>
using namespace std;

class Player
{
private:
	string playerName;
	int playerPoints;

public:
	Player();
	Player(string name);
	string getName();
	void setName(string name);
	int getPoints();
	void setPoints(int points);
}; 


#endif 


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
#include<iostream>
#include "Player.h"
using namespace std;

Player::Player(){
	playerPoints = 0;
}

Player::Player(string name) {
	playerName = name;
}

string Player::getName(){
	return playerName;
}

void Player::setName(string name){
	playerName = name;
}

int Player::getPoints(){
	return playerPoints;
}

void Player::setPoints(int points){
	playerPoints = points;
}



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

#include<iostream>
#include <string>
#include<vector>
#include <fstream>
#include "Player.h"
#include "Card.h"
using namespace std;

void gameMenu();
void gameRules();
void gameStart();
void gamePlay(vector <Player>& );
void printPlayers(vector <Player>&);


int main()
{
	gameMenu();

	return 0;
}

void gameMenu()
{
	cout << "Welcome to Pairs " << endl;
	cout << "Select Menu Option " << endl;
	cout << "1. Game Rules" << endl;
	cout << "2. Play Game " << endl;

	int num;
	cin >> num;

	switch (num)
	{
	case 1:
		gameRules();
		break;
	case 2:
		gameStart();
		break;
	default:
		cout << "invalid " << endl;
	}

	system("pause");
	system("CLS");
}

void gameRules()
{
	cout << "Here are the basic rules of the game" << endl;

	string line;
	fstream rulesFile;
	rulesFile.open("Gamerules.txt");

	if (rulesFile.is_open())
	{
		{
			while (getline(rulesFile, line))
			{
				cout << line << endl;
			}
		}
	}
	rulesFile.close();

	system("pause");
	system("CLS");

	gameMenu();
}

void gameStart() {
	vector <Player> playerclass;
	gamePlay (playerclass);
	printPlayers(playerclass);
}
void gamePlay(vector <Player>& newPlayerclass)
{
	cout << "How many players in the game?" << endl;
	int x;
	cin >> x;

	for (int i = 0; i < x; i++) {
		string n;
		cout << "Enter name: " << endl;
		cin >> n;

		Player newPlayers(n);
		newPlayerclass.push_back(newPlayers);
	}
	
	system("pause");
	system("CLS");

}

void printPlayers(vector <Player>& newPlayerclass) {
	
	unsigned int y = newPlayerclass.size();
	for (unsigned int i = 0; i < y; i++) {
		cout << newPlayerclass[i].getName() << newPlayerclass[i].getPoints() << endl;
	}
}
Last edited on
Hi,

Why dont you just declare their points = 0 in the private variables? You probably could try and make a copy constructor as well, something like

prototype
Player(string, int)

overloaded copy constructor
Player::Player(string name, int points)
{
// rest of stuff in here.
}

do you mean declare points = 0 in the header files?
You never set playerPoints anywhere once the object is constructed. You only set it in the default constructor, which you never use. Thus, the call in line 104 is garbage.
i guess this is what you ment. Thank you so much

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void gamePlay(vector <Player>& newPlayerclass)
{
	cout << "How many players in the game?" << endl;
	int x;
	cin >> x;

	for (int i = 0; i < x; i++) {
		string n;
		int p=0;
		cout << "Enter name: " << endl;
		cin >> n;

		Player newPlayers(n,p);
		newPlayerclass.push_back(newPlayers);
	}
	
	system("pause");
	system("CLS");

}
Last edited on
You don't have a constructor that takes both a std::string and an int either, so unless you added one that would fail to compile.
Hello saliya1964,

Something you could do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void gamePlay(vector <Player>& newPlayerclass)
{
	cout << "How many players in the game?" << endl;
	int x;
	cin >> x;

	for (int i = 0; i < x; i++)
{
	        string name;

		cout << "Enter name: " << endl;
		cin >> name;

		newPlayerclass.emplace_back(name, 0);
	}
	
	system("pause");
	system("CLS");

}


And the overloaded ctor would be:
1
2
3
4
5
6
7
8
9
// <--- forward declaration
Player(string name, int points);

// <--- Function
Player::Player(string name, int points)
{
	playerName = name;
        playerPoints = points;
}

The ".emplace_back" function will call the overloaded ctor to construct an object of the class before it puts it into the vector.

Andy
thank you andy. it was really helpful
Topic archived. No new replies allowed.