I am in my first year of coding in school. While on break I am continuing to learn C++ on my own. This is not a finished product due to the fact that I haven't wrote the function to keep score of the money earned or lost. I am trying to see if this code that I have written is efficient. Any input would be greatly appreciated.
cout << "Your new Hand is: \n";
for(int i = 0; i < HAND; i++)
{
cout << setw(3) << cardHand[i] << " ";
cout << suit[i] << endl;
}
cout << endl;
}
void exchangeCards(int fiveCardHand[], vector<int>& deck, int& sizeOfDeck, char suit[])
{
int howManyToSwap;
int cardToSwap, card;
vector<int> swapTheseCards;
if(wantToSwapcards())
{
do
{
cout << "How many cards do you want to swap? ";
cin >> howManyToSwap;
if(howManyToSwap < 1 || howManyToSwap > 5)
cout << "Invalid Entry!! Must be between 1-5\n";
}while(howManyToSwap < 1 || howManyToSwap > 5);
if(howManyToSwap > 1 && howManyToSwap < 5)
{
cout << "Enter the card(s) number (1-5) you want to exchange with a space between them. \n";
cout << "Swap card(s) number : ";
for(int i = 0; i < howManyToSwap; i++)
{
cin >> cardToSwap;
swapTheseCards.push_back(cardToSwap);
}
}
}
for(int i = 0; i < swapTheseCards.size(); i++)
{
card = deck[sizeOfDeck] % 13 + 1;
int num;
num = swapTheseCards[i];
num = num - 1;
fiveCardHand[num] = card;
suit[num] = deck[sizeOfDeck] % 4 + 3;
deck.pop_back();
--sizeOfDeck;
}
}
bool wantToSwapcards()
{
char answerSwap;
bool answer = false;
do
{
cout << "Do you want to swap any of your cards? (Y or N) ";
cin >> answerSwap;
if(!(answerSwap == 'y' || answerSwap == 'Y' || answerSwap == 'n' || answerSwap == 'N'))
cout << "Invalid entry enter Y or N ONLY!!\n";
}while(!(answerSwap == 'y' || answerSwap == 'Y' || answerSwap == 'n' || answerSwap == 'N'));
Also you're passing array's around from function to function. That means the entire array needs to be copied into a new memory space. You should pass using pointers. For example:
That means the entire array needs to be copied into a new memory space. You should pass using pointers.
Alas, that is not the case.
Arrays are passed by pointer -- the array name degenerates to a pointer, and the argument int cardHand[] is itself a pointer -- it could have been written with the equivalent int *cardHand.