getName

hi im making an get name function to get my players names i get the name outputs i just wanna know if i done it properly :)

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
class player {

public:
	string name;
	int score;
	void getName(string& player1, string& player2)
	{

		cout << "***************************************" << endl;
		cout << "        LETS GET STARTED !!!!          " << endl;
		cout << "***************************************" << endl;
		cout << "     ENTER PLAYER 1 NAME :- ";
		cin >> player1;
		cout << "     ENTER PLAYER 2 NAME :- ";
		cin >> player2;
		cout << "***************************************" << endl;
		cout << "Hello!! " << player1 << " and " << player2 << " 
                 welcome to PAIRS!!" << endl;
	}
//THIS PART IS IN THE MAIN	
};

  int playGame(){
	player getAll;
	string name1;
	string name2;
	getAll.getName(name1,name2);
	system("pause");
	system("cls");
	cout << "**********************************************"<<endl;
	cout << "NAME:- " << name1 << " Score:- "<<endl;
	cout << "**********************************************" << endl;
	cout << "NAME:- " << name2 << " Score:- "<<endl;
	return 0;

}
Last edited on
Strictly speaking, the answer is no. It's not correct.
Did you try compiling this (with necessary #includes and main function)?

Line 17-18: You can't break the quotes like that over multiple lines (well you can, but just don't). Bring the " down to line 18.

But... syntax aside, we have no idea what "correct" means.
***************************************
        LETS GET STARTED !!!!          
***************************************
     ENTER PLAYER 1 NAME :- John
     ENTER PLAYER 2 NAME :- Connor
***************************************
Hello!! John and Connor
welcome to PAIRS!!
**********************************************
NAME:- John Score:- 
**********************************************
NAME:- Connor Score:- 

It look OK. I assume you are going to fill in "score" with something?

But the name of your class is odd. You call it player, but you use this class to fill in two strings. You don't use the name or score member variables of your class.

getName() might as well just be a function defined outside of any class, because the class is not used in any meaningful way.

Focus on the actual logic of the program before focusing on eye-candy like clearing the screen and "**************".
Last edited on
im really sorry i couldnt use it clearly the second int playGame()is what i have used in the main it was working
and i will remove the syntax breaking to thankyou :)
I'm not sure what you're sorry for, I'm just replying to your initial question.

If the program does what you want it to do, then great... but I have no idea what the actual requirements of the program are, so it's hard to help.
Last edited on
its a game of cards but its not a normal game of cards its a card game called pairs in their game
-deck has 55 cards(1(1),2,(2),3(3)....10(10))
-the whole point of the game is not to reach the target score

-score
for a two player game the score is 31

-how do the score go up
in the game there are two options either hit or fold

-hit , this means you can draw a card from the deck but if you draw and get a pair that cards number will add up to your score and the hand gets discarded and they get dealt a new card this happens when ever a round ends.


-a round ends when a player either decides to fold or gets a pair

-a pair, a pair is when you get the same number when you hit and draw a card
example :- [10] [10]

-fold, this is so if you have a higher value number such as [10] there is a high chance of getting a number [10] so the player can fold and get the lowest card in the other players hand and then again the hand gets discarded and the card he took from folding will be added to the score

-if the deck runs out the discarded has to be used again by burning the top 5 cards of the discarded deck
- game ends when a player reaches a target score so this has to be checked every round

so to make this game i should use three classes,
class class
class player
class pairs and this includes the main()

and in the uml diagrams i did watching an example they gave for an other game,
Player
-playerName : string
-playerScore : int
+Player(name:string)
+~Player()
+ getName():string
+ getScore(): int
+ updateScore():void

Card
- cardValue :int
+ deckOfCards() :int
+~deckofCards()
+ randShuffle() : int
+ discardedCards() :int
+ burnCards() : void
+ resetDeck() : int

Pairs
Players : vector<player>
Cards : vector <card>
targetScore : int
main()
menu() : void
addPlayers() :
void displayHowToPlay(filename:string): void
playGame() : void

menueOption(option:int) : int
getNoOfPlayers(number:int) :int //this is an additional task you can add more players to
play
stringGetName(name:string):string
getOption(option:char):int
playAgain(answer:char):bool
checkLoser(): bool

(the other player is not an AI its me playing vs me for the moment :) )

i can post the code of what i have done to if you want . and im sorry if i have made any silly mistakes cus this is my first major project as in ive never made anything like this,feel free to share your ideas :)
Last edited on
Player
-playerName : string
-playerScore : int
+Player(name:string)
+~Player()
+ getName():string
+ getScore(): int
+ updateScore():void

Yes, so as I suspected, you are using the Player class incorrectly.

The player class should have member variables "playerName" and "playerScore".
It should have a constructor that takes in a string for the name (presumably, starting the score at 0).
And then also has getters and setters, and an updateScore() function, although I'm not sure what the function of this is, seeing as it apparently doesn't take in any parameters. Maybe it should take in an int?

Something like:

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
class Player {
  public:
    Player(string name)
    {
        playerName = name;
        playerScore = 0;
    }
    ~Player() { } // not needed, but your assignment wants it

    string getName()
    {
        return playerName;
    }

    int getScore()
    {
        return playerScore;
    }

    void updateScore(int newScore) // guessing here
    {
        score = newScore; // guessing here
    }

  private:
    string playerName;
    int playerScore;
};
Last edited on
thank you Ganado, i get where i have went wrong but i dont know how to use thes to get the player inputs in the main im sorry if its a bad qustion :) the reason me doing it that way is thats the only way i knew i could use them to get an input can you explain me how can i use this in my programme?
Last edited on
Maybe like this:
1
2
3
4
5
6
7
8
9
10
int main()
{
   string name;
   cout << "Enter player name: ";
   getline("cin, name);
   // Now you can create your player
   Player player1(name);
   
   cout << "The player name: " << player1.getName() << '\n';
} 
what can i add inside the constructor? :)
Look at Ganado's latest post. I think there is nothing to add.
Topic archived. No new replies allowed.