I can't make my array work. I have never encountered this error before and I am a beginner programmer, this is my homework assignment. Please help! I really don't understand/know a lot of terminology, so it would be most helpful if you showed me how to change my errors. Thank you!
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
void drawCard(int &value, int &suit, int drawn[])
{
do
{
value = (rand()%13) + 1;
suit = (rand()%4) + 1;
} while (drawn[(value*suit) - 1] == 1);
drawn[(value*suit) - 1] = 1;
}
void displayCardValue(int value) // so card values 1,
//11-13 correspond to card names
{
if ((value >= 2) && (value <= 10)) // cards 2-10 are numbers
cout << value;
else if (value == 1)
cout << "Ace";
else if (value == 11)
cout << "Jack";
else if (value == 12)
cout << "Queen";
else if (value == 13)
cout << "King";
else
{
cout << "Error! Invalid card value." << endl;
exit(0);
}
}
void displayCardSuit(int suit) // assign numbers 1-4 to 1 of the 4 suits
{
if (suit == 1)
cout << " of spades";
else if (suit == 2)
cout << " of clubs";
else if (suit == 3)
cout << " of diamonds";
else if (suit == 4)
cout << " of hearts";
else
{
cout << "Error! Invalid card suit." << endl;
exit(0);
}
}
void displayCard(int &value, int &suit, int drawn[]) // so don't have to call both
//displayCardFace and displayCardSuit, can call both at once
{
drawCard(value, suit, drawn);
displayCardValue(value);
displayCardSuit(suit);
}
int getCardPoints(int &value, int &aceValue)// assigning values to cards for
//black jack addition
{
if ((value >= 2) && (value <= 10))
return (value);
else if (value == 1)
{
aceValue = 10; //11-1
return 1; // see explanation
}
else if (value == 11)
return 10;
else if (value == 12)
return 10;
else if (value == 13)
return 10;
else if (value == 0)
return 0;
else
{
cout << "Error! Invalid card value." << endl;
exit(0);
}
}
int playerLoop(int playerTotal, int & aceValue, int drawn[]) //for players points and if
//they want to keep getting new cards
{
string answer;
do // loop because player may be asked to hit or stay multiple times
{
cout << "Player's current hand point value is ";
cout << playerTotal;
if (aceValue > 0)
cout << " or " << playerTotal + aceValue << endl;
else
cout << endl; //added in myself
cout << "Do you want another card? (\"hit\" or \"stay\"): ";
getline(cin, answer);
if (answer == "hit")
{
int nextValue = 0; //(rand()%13) + 1; //gets value number
int nextSuit = 0; //(rand()%4) + 1; //gets a suit
cout << "Player is dealt ";
displayCard(nextValue, nextSuit, drawn); //calls and displays the card
cout << endl;
//add new points onto old point total for a new total
playerTotal = playerTotal + getCardPoints(nextValue, aceValue);
}
} while ((answer == "hit") && (playerTotal <= 21)); //over 21 lose or at 21 win
return playerTotal; //returns new player total
}
int dealerLoop(int dealerTotal, int & aceValue, int drawn[])
{
int bestHand;
if (dealerTotal + aceValue < 22)
bestHand = dealerTotal + aceValue;
else
bestHand = dealerTotal;
while (bestHand < 17)
{
cout << "Dealer's current hand point value is ";
cout << dealerTotal;
if (aceValue > 0)
cout << " or " << dealerTotal + aceValue;
cout << endl;
int nextValue = 0; //(rand()%13) + 1; //gets face number
int nextSuit = 0; //(rand()%4) + 1; //gets a suit
cout << "Dealer is dealt ";
displayCard(nextValue, nextSuit, drawn); //calls and displays the card
cout << endl;
//add new points onto old point total for a new total
dealerTotal = dealerTotal + getCardPoints(nextValue, aceValue);
if (dealerTotal + aceValue < 22)
bestHand = dealerTotal + aceValue;
else
bestHand = dealerTotal;
}
return dealerTotal; //returns dealer's new total
}
int main()
{
// Reset the random number generator
srand((unsigned)time(0));
// draw a card
int playerC1_value = 0; //(rand()%13) + 1;
int playerC1_suit = 0; //(rand()%4) + 1;
int dealerC1_value = 0; //(rand()%13) + 1;
int dealerC1_suit = 0; //(rand()%4) + 1;
int playerC2_value = 0; //(rand()%13) + 1;
int playerC2_suit = 0;//(rand()%4) + 1;
int dealerC2_value = 0; //(rand()%13) + 1;
int dealerC2_suit = 0; //(rand()%4) + 1;
//player dealt 2 cards, know both values
int value = 0, suit = 0;
int drawn[52] = {0};
int index = (value * suit)-1;
drawn[index] = 1;
cout << "Player has ";
displayCard(playerC1_value, playerC1_suit, drawn);
drawn[index] = 1;
cout << " and ";
displayCard(playerC2_value, playerC2_suit, drawn);
cout << endl;
//dealer dealt 2 cards, only display 1
cout << "Dealer has ";
displayCard(dealerC1_value, dealerC1_suit, drawn);
cout << " showing " << endl;
drawCard(dealerC2_value, dealerC2_suit, drawn);//draws a card for the second dealer card value
// figure player total hand value
int playerAceValue = 0;
int dealerAceValue = 0;
int playerTotal = getCardPoints(playerC1_value, playerAceValue) + getCardPoints(playerC2_value, playerAceValue);
playerTotal = playerLoop(playerTotal, playerAceValue, drawn);
int dealerTotal = getCardPoints(dealerC1_value, dealerAceValue) + getCardPoints(dealerC2_value, dealerAceValue);
dealerTotal = dealerLoop(dealerTotal, dealerAceValue, drawn);
if (playerAceValue >= 1)
cout << "At the end, player has " << playerTotal << " or " << playerTotal + playerAceValue << " points " << endl;
else
cout << "At the end, palyer has " << playerTotal << " points" << endl;
if (dealerAceValue >= 1)
cout << "At the end, dealer has " << dealerTotal << " or " << dealerTotal + dealerAceValue << " points " << endl;
else
cout << "At the end, dealer has " << dealerTotal << " points" << endl;
int playerHighestTotal = playerTotal + playerAceValue;
int dealerHighestTotal = dealerTotal + dealerAceValue;
if ((dealerTotal == playerTotal) || (dealerTotal == playerHighestTotal)
|| (dealerHighestTotal == playerTotal) || (dealerHighestTotal == playerHighestTotal))
cout << "Draw" << endl;
else if (((dealerTotal == 21) && (playerTotal == 21)) || ((dealerTotal == 21) && (playerHighestTotal == 21))
|| ((dealerHighestTotal == 21) && (playerTotal == 21)) || ((dealerHighestTotal == 21) && (playerHighestTotal == 21)))
cout << "Tie!" << endl;
else if ((playerTotal > 21) && (playerHighestTotal > 21))
cout << "Player busted" << endl;
else if (((dealerTotal > 21) && (dealerHighestTotal > 21)) && ((playerTotal <= 21) || (playerHighestTotal <= 21)))
cout << "Dealer busted -- player wins!" << endl;
else if (((dealerTotal > playerTotal) && (dealerTotal > playerHighestTotal)) || ((dealerHighestTotal > playerTotal) &&
(dealerHighestTotal > playerHighestTotal)) && ((dealerHighestTotal <= 21) || (dealerTotal <= 21)) && ((playerHighestTotal < 21)
|| (playerTotal < 21)))
cout << "Dealer wins" << endl;
else
cout << "Player wins!" << endl;
}