0
while (deckSize > 2)
{

    one_Card = card_deck.back();
    card_deck.pop_back();

    two_Card = card_deck.back();
    card_deck.pop_back();

    three_Card = card_deck.back();
    card_deck.pop_back();


    oneCard_Name = card_name(one_Card);
    twoCard_Name = card_name(two_Card);
    threeCard_Name = card_name(three_Card);

    oneCard_Suit = card_suit(one_Card);
    twoCard_Suit = card_suit(two_Card);
    threeCard_Suit = card_suit(three_Card);

    oneCard_Rank = card_rank(one_Card);
    twoCard_Rank = card_rank(two_Card);
    threeCard_Rank = card_rank(three_Card);

    bool between1 = (oneCard_Rank < threeCard_Rank && threeCard_Rank < twoCard_Rank);
    bool between2 = (twoCard_Rank < threeCard_Rank && threeCard_Rank < oneCard_Rank);


    cout << "Here are your two cards: " << endl;
    cout << setw(10) << oneCard_Name << " of " << oneCard_Suit << setw(20) << twoCard_Name << " of " << twoCard_Suit << endl;
    cout << "Do you think the next card will lie between these? (y/n): ";
    cin >> user_input;
    cout << endl << endl;
    cout << "Here is your next card: " << endl;
    cout << setw(10) << threeCard_Name << " of " << threeCard_Suit << endl << endl << endl;
    count++;


    if(user_input == "y" || user_input == "yes" || user_input == "Yes" || user_input == "Y")
    {
        if(between1 || between2)
        {
            cout << "You win!" << endl;
            win++;
        }
        else
        {
            cout << "You lose!" << endl;
            lose++;
        }
    }
    else
    {
        if(between1 || between2)
        {
            cout << "You lose!" << endl;

            lose++;
        }
        else
        {
            cout << "You win!" << endl;

            win++;
        }
    }
}


cout << "You have played this game " << count << " times and you have won: " << win << " and lost " << lose << endl;

return 0;

}

These are the two subprograms that shuffle and initialize the deck

void initDeck(vector<int> &card_deck)
{
    int i;
    for(i = 0; i <= 51; i++)
    {
        card_deck[i] = i;
    }

}


void shuffleDeck(vector<int> & card_deck)
{
    int n;

        for(n = 51; n >= 0; n--)
        {
            int i = randomize();
            int temp = card_deck[i];
            int temp2= card_deck[n];
            card_deck[n] = temp;
            card_deck[i] = temp2;

        }

}

After when I run the program it allows me to run it, but when I reach to the number less than the condition in the while loop it just gives me an error, and does not finish the program. I had this error earlier and fixed it, so I have a basic understanding of what the error means. From my knowledge it is trying to collect numbers past the vector length. However this time I don't see my error at all.

John
  • 3
  • 1
  • Your `initDeck` looks dangerously wrong. Hae it return a `std::vector` instead of taking one by reference, or at least ensure the `vector` is of sufficient size. Code should check its preconditions, or at least assert them. – Yakk - Adam Nevraumont Mar 12 '14 at 01:53

3 Answers3

1

deckSize is not being set/updated anywhere. It should rather be card_deck.size()

SleuthEye
  • 14,379
  • 2
  • 32
  • 61
  • Oh sorry, it is before my while loop. But does it make a difference if I set in deckSize = card_deck.size(). That's what I did. – John Mar 12 '14 at 02:46
  • Doesn't make a difference so long as it is assigned before and updated _every_ loop iteration, not just before. – SleuthEye Mar 12 '14 at 02:48
  • Oh my god thank you so much!!!! It finally works, okay I see what you mean, the reason my program kept looping is because when I assigned sizeDeck in the beginning it did not change so the loop was always true – John Mar 12 '14 at 03:01
0

You should use push_back and emplace for the type vector like this:

void initDeck(vector<int> &card_deck){
int i;
for(i = 0; i <= 51; i++)
{
    card_deck.push_back(i);
}

}

Take a see to this link

0

Try this:

void initDeck(vector<int> &card_deck)
{
    int i;
    for(i = 0; i <= 51; i++)
    {
        card_deck.push_back(i);
    }

}


void shuffleDeck(vector<int> & card_deck)
{
    int n;

        for(n = 51; n >= 0; n--)
        {
            int i = randomize();
            int temp = card_deck[i];
            int temp2= card_deck[n];
            card_deck[n] = temp;
            card_deck[i] = temp2;

        }

}

For generating random number see this and this. Or you can find other solution that is more reliable.

Community
  • 1
  • 1
Rashad
  • 11,057
  • 4
  • 45
  • 73
  • I was originally using the push back however it does stop my while loop. After the 17th iteration it will continue to draw the same cards over and over again. – John Mar 12 '14 at 02:12
  • it because random number is being repeated. – Rashad Mar 12 '14 at 02:35
  • Thanks, for all the help. But I don't see how that is give me a vector error after my while loop. Isn't my random number just give me numbers to put into my int i then stopping. I run the program but now its stopping after 17th again, give me the vector error. – John Mar 12 '14 at 02:48