I'm new to C++ and trying to make a Blackjack simulator. The Player is a class that stores cards dealt from the deck; class Card contains a suit value and a face value. I keep getting error message "Control may reach end of non-void function" for Card Player::getCard( int index ) const, what did I do wrong? Also can someone check if there is any logic flaws in my code since I can't run it and check because of the error message?
#include "Player.h"
#include "Game.h"
#include "Card.h"
using namespace std;
Player::Player( )
{
// The Player has no Cards in his hand
myNumberOfCards = 0;
}
std::ostream& operator <<( std::ostream& outs, const Player & p )
{
// print out all the actual cards in the array myCards
for (int i = 0; i < p.cardCount(); i++)
{
outs << p.myCards[i] << endl;
}
return( outs );
}
void Player::acceptCard(Card c)
{
// as long as there is space in the array myCards, place Card c into myCards
// if there is not enough space for another card, throw an exception
try
{
for (; myNumberOfCards < MAXCARDS; myNumberOfCards++)
myCards[ myNumberOfCards ] = c;
if (myNumberOfCards > MAXCARDS)
throw myNumberOfCards;
}
catch (int e)
{
std::logic_error( "more than maximum of cards possible" ); // Since the player must be busted if he has more than 11 cards, how should I set the outcome to playerbusted if I have a bool in the game class?
}
}
Card Player::getCard( int index ) const
{
// return the requested card
// if the index is bad, throw an exception
try
{
while ( index > 0 && index < myNumberOfCards )
return ( myCards[ index ] );
if (index < 0 || index > myNumberOfCards)
throw index;
}
catch (int e)
{
std::logic_error( "bad index" ); // why there's an error?
}
}
int Player:: cardCount() const
{
// return the number of cards stored in my array
return myNumberOfCards;
}
int Player::handcount( ) const
{
// total up the points in this player's hand
// Ace's might be worth 1 or 11
Player p;
int total = 0;
bool hasAce = false;
for (int i = 0; i < myNumberOfCards; i++)
{
total += myCards[i].count();
if (myCards[i].getFace() == ACE)
hasAce = true;
}
if (total < 11 && hasAce == true)
total += 10;
return( total );
}
bool Player::hasBlackJack( ) const
{
bool result = false;
if (myNumberOfCards == 2 && handcount() == 21)
{
result = true;
}
return( result );
}