I cant store the passed in value in a method

Write your question here.

ok so my issue is in the AddCard method, where im trying to store the value but I cant figure how to do it.



[/code]
class Player
{

protected:

// Needed for unit tests
// DO NOT REMOVE OR RENAME

/* TODO Lab4:
Turn the name and hand data members into dynamic arrays.
*/
char m_name[32]; // Can change size of array if needed
Card m_hand[7]; // Can change size of array if needed

int m_numCards; // The number of cards the player is currently holding

/* TODO Lab5:
Update m_maxCards to be a const data member
*/
int m_maxCards; // The number of cards the player can store (the number of elements in Hand)
int m_score; // For "Go Fish," this will represent the number of pairs. For "UNO," it will be the player's accumulated score

public:

// Default ctor
// In: _name The player's name
// _maxCards The maximum number of cards they can store

/* TODO Lab2: /Done/
Make the default values "Player" and 7
*/
Player(const char* _name = "Player", int _maxCards = 7);

/* TODO Lab4:
Prototype the copy constructor
*/


// Dtor
/*
TODO Lab2:/Done/
Make sure that all destructors are called in the case of upcast objects.
*/
virtual ~Player();

/* TODO Lab4:
Prototype the assignment operator
*/

/* Accessors */

/* TODO Lab2:/Done/
Define the "Get" methods. These should return the
corresponding values from the invoking object.

These can be inlined if desired.
*/

const char* GetName() const
{
return m_name;
}

int GetNumCards() const
{
return m_numCards;
}

int GetMaxCards() const
{
return m_maxCards;
}

int GetScore() const
{
return m_score;
}

// Do not inline this next method

// Access a Card from the player's hand
// In: _index The index of the card being accessed
// _card A reference to store that value in
//
// Return: True if there was a card actually stored at that index.
// False if the index was "empty"
bool GetCard(int _index, Card& _card) const;

/* Mutators */

// Change the player's name to the value passed in
void SetName(const char* _name);

// Update the player's score by some amount
void AddToScore(int _add);

// Add a Card to the player's hand
// In: _card The card being added
//
// Return: True if the card was successfully added to their hand
// False if the player's hand was already full
bool AddCard(const Card& _card);

// Remove a Card from the player's hand
// In: _index The index of the card to remove from the array
// _card A reference to store that value in
//
// Return: True if there was a card actually stored at that index.
// False if the index was "empty"
bool Discard(int _index, Card& _discard);

////////////////////////////////////////////////////////////////////////////////
This is in the player.cpp(Player is a class)
// Add a Card to the player's hand
// In: _card The card being added
//
// Return: True if the card was successfully added to their hand
// False if the player's hand was already full
bool Player::AddCard( const Card& _card)
{
/* TODO Lab2:
Implement this method.
Should store the passed card in the next available empty index (if there is one).
You should know how many cards are in the hand to know where that index will be.
*/




if (m_numCards < m_maxCards)
{
m_hand[m_numCards + 1] = _card;
return true;
}
else if (m_numCards >= m_maxCards)
return false;


}
[/code]
Last edited on
You are storing it. You just forgot to update m_numCards.
@MiiNiPaa how would i update m_ numcards?
calling the GetNumCards()?
@MiiNiPaa how would i update m_ numcards?
As any other variable:

variable = new_value
im srry, but I dont understand.
In AddCard you add a card into an array, so m_numCards is now not reflect reality. You need to update it.

Now think, how many cards are in array now after we add one. Then assign that value to m_numCards
if (m_numCards < m_maxCards)
{
m_hand[m_numCards + 1] = _card;
m_numCards = m_numCards + 1;
return true;
}
else if (m_numCards >= m_maxCards)
return false;


I did this but still doesnt work
What exactly does not work? How do you check it?

Some other potential problems: is GetCard implementation correct? Do you set m_numCards to 0 at construction?
ok this solution has like some kind of function that the teacher wrote so we could test them
and I get this, what I did kind of fixed some of the things cause before i was getting over 14 failures now Im just getting 4.

Testing Player::AddCard
Adding 5 random cards to the hand. Ensuring that they go to the right index.
e:\full sail\pg2\pg2 project\card game\lab2_test.cpp(441): error: Failure in PG2_Lab2_Player_AddCard: Expected 8
but was 2

e:\full sail\pg2\pg2 project\card game\lab2_test.cpp(441): error: Failure in PG2_Lab2_Player_AddCard: Expected K 
but was 8

e:\full sail\pg2\pg2 project\card game\lab2_test.cpp(441): error: Failure in PG2_Lab2_Player_AddCard: Expected A 
but was K 

e:\full sail\pg2\pg2 project\card game\lab2_test.cpp(441): error: Failure in PG2_Lab2_Player_AddCard: Expected 6
but was A 

Making sure adding any cards past MaxCards fails.
FAILURE: 1 out of 1 tests failed (4 failures).
Test time: 0.01 seconds.


Press any key to continue . . .
strcpy_s(m_name, 32, _name);
m_maxCards = _maxCards;
m_numCards = 0;
m_score = 0;


this is the player class default constructor, so yeah, m_numCards is set to 0
ok I fixed it but now I get a warning that says that the AddCard: not all control paths have value
it simply means that the method scope does not return a value by default in case the thread does not go within a condition/statement scope (inner scope) .
remove if from else statement. It is not needed here.
if A
    B
else if ...
    C
alright thanks guys, it is fixed
Topic archived. No new replies allowed.