I'm making a blackjack game and my vector for my "hand" isn't working. Every time I run it, it says "vector subscript out of range" and I can't seem to find the error
void draw(){ blackjack.cardslot++; } void BLACKJACKpickbid(){ cout << "Enter bid :)))))" << endl; cin >> blackjack.bidamount; if (blackjack.bidamount > blackjack.playerbank){ cout << "Not enough money :(" << endl; } } //used to pick bids void BLACKJACKnewround(){ shuffledeck(); blackjack.cardslot = 0; blackjack.playerhandvalue = 0; blackjack.dealerhandvalue = 0; blackjack.playerturn = true; for (blackjack.playerx; blackjack.playerx >= 0; blackjack.playerx--){ general.hand.pop_back(); } for (blackjack.dealerx; blackjack.dealerx >= 0; blackjack.dealerx--){ blackjack.dealerhand.pop_back(); } blackjack.dealerx = 0; blackjack.playerx = 0; //check all values BLACKJACKpickbid(); } // used to start new round void BLACKJACKplayerwin(){ blackjack.playerbank += blackjack.bidamount; BLACKJACKnewround(); } // used if player wins void BLACKJACKplayerlose(){ blackjack.playerbank -= blackjack.bidamount; BLACKJACKnewround(); } // used if player loses void BLACKJACKcompare(){ if (blackjack.playerhandvalue < 21 && blackjack.dealerhandvalue < 21){ if (blackjack.playerhandvalue > blackjack.dealerhandvalue){ BLACKJACKplayerwin(); // ADD TO PLAYER BANK THE AMOUNT BID. NEW ROUND } if (blackjack.playerhandvalue == blackjack.dealerhandvalue){ BLACKJACKnewround(); } else { BLACKJACKplayerlose(); //SUBTRACT FROM PLAYER BANK AMOUNT BID. NEW ROUND } } if (blackjack.playerhandvalue < 21 && blackjack.dealerhandvalue > 21){ BLACKJACKplayerwin(); } if (blackjack.playerhandvalue > 21 && blackjack.dealerhandvalue < 21){ BLACKJACKplayerlose(); } if (blackjack.playerhandvalue > 21 && blackjack.dealerhandvalue > 21){ BLACKJACKnewround(); } } // used to determine winner/loser void BLACKJACKdealerbeginning(){ draw(); blackjack.dealerhand.push_back(general.deck[blackjack.cardslot]); blackjack.dealerhandvalue += blackjack.dealerhand[blackjack.dealerx]; blackjack.dealerx++; draw(); blackjack.dealerhand.push_back(general.deck[blackjack.cardslot]); blackjack.dealerhandvalue += blackjack.dealerhand[blackjack.dealerx]; blackjack.dealerx++; } //used at beginning to initialize first 2 cards note: have to make graphics void BLACKJACKplayerbeginning(){ draw(); general.hand.push_back(general.hand[blackjack.cardslot]); blackjack.playerhandvalue += general.hand[blackjack.playerx]; blackjack.playerx++; draw(); general.hand.push_back(general.hand[blackjack.cardslot]); blackjack.playerhandvalue += general.hand[blackjack.playerx]; blackjack.playerx++; } //used at beginning to initialize first 2 cards note: have to make graphics void BLACKJACKdealersturn(){ while (blackjack.playerturn == false){ BLACKJACKdealerbeginning(); if (blackjack.dealerhandvalue > 17){ BLACKJACKcompare(); } else{ draw(); blackjack.dealerhand.push_back(general.deck[blackjack.cardslot]); blackjack.dealerhandvalue += blackjack.dealerhand[blackjack.dealerx]; blackjack.dealerx++; } } } //dealer goes second void BLACKJACKstand(){ blackjack.playerturn = false; BLACKJACKdealersturn(); } void BLACKJACKplayersturn(){ while (blackjack.playerturn == true){ BLACKJACKplayerbeginning(); if (blackjack.hit == true){ draw(); general.hand.push_back(general.hand[blackjack.cardslot]); blackjack.playerhandvalue += general.hand[blackjack.playerx]; blackjack.playerx++; } else{ if (blackjack.sstand == true){ BLACKJACKstand(); } } } } //player goes first void BLACKJACK(){ BLACKJACKplayerbeginning(); BLACKJACKdealerbeginning(); BLACKJACKplayersturn(); } void main(){ cout << "Hello World! pls save me :( " << endl; BLACKJACK(); system("pause"); }
sorry this has nothing to do with the blackjack..but try avoiding system("pause") instead use return 0;
I don't feel like reading all that code. Two things could cause the subscript out of range: the vector is too short or the subscript is too big. Put some code in to print the vector's length after it's populated and just before the call that's throwing the error. If you get the expected value, then check the subscript before use.
what would a subscript be in a vector, sorry just confused btw, thx for tip immanuelv
The subscript is the number in: vector[ 1 ]; // to access the second element of vector http://www.cplusplus.com/reference/vector/vector/?kw=vector
1. Do you have more details on the error message? Does it give you a line number where it's falling over? If not, try inserting some basic debugging 'cout' lines to trace precisely where the error is occurring. 2. Tracing the flow, the first thing that seems to be happening is that main() calls BLACKJACK() which calls BLACKJACKplayerbeginning() which calls draw() which increments blackjack.cardslot. My first thought at that point was "is blackjack.cardslot initialized?"... Could that be causing the error? Do you need to call BLACKJACKnewround() before BLACKJACKplayerbeginning()?
yes, blackjack.cardslot is initialized at the beginning. However, I did figure out the error. I was using hand.push_back(hand) instead of hand.push_back(deck). Thanks for the help guys
Join our real-time social learning platform and learn together with your friends!