Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8591883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:37:25+00:00 2026-06-11T23:37:25+00:00

I made this very simple solitaire in C++. The deck is random shuffled, I

  • 0

I made this very simple solitaire in C++. The deck is random shuffled, I take a card and I pronunce in sequence Ace, One, Two, …, Ten, Jack, Queen, King. If the card is exactly what I pronunced I remove the card from the deck. I iterate the process using the remained deck until I remove cards or until the deck is empty (only in this case I win). This is the code I write but I’ve an error in the line 62 when I use the function erase to remove a card. I can’t understand which is the problem.

#include <iostream>
#include <vector>
using namespace std;

enum SUIT { HEART, CLUB, DIAMOND, SPADE };
string suit_string[] = {"HEART", "CLUB", "DIAMOND", "SPADE"};

enum VALUE { ACE, TWO, THREE, FOUR, FIVE, SIX,
    SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING};
string value_string[] = {"ACE", "TWO", "THREE", "FOUR", "FIVE",
    "SIX", "SEVEN", "EIGHT","NINE", "TEN", "JACK", "QUEEN", "KING"};

class Card {
 public:
    Card(SUIT s, VALUE v);
    SUIT suit;
    VALUE value;
    string toString();
};

Card::Card(SUIT s, VALUE v){
    suit=s;
    value=v;
}

string Card::toString(){
    return value_string[(int)this->value]+" of "+suit_string[(int)this->suit];
}



int main() {
        vector<Card*> deck;
        for (int i=0; i<4; i++) {
            for (int j=0; j<13; j++) {
                deck.push_back(new Card((SUIT)i,(VALUE)j));
            }               
        }

        int removed_count, runs = 0;

        do{
            random_shuffle(deck.begin(),deck.end());
            removed_count=0;

            cout<<"--- Run number: "<<++runs<<"\n";
            cout<<"--- still "<<deck.size()<<" cards in the deck\n";

            for (int i=0; i<4; i++) {
                for (int j=0; j<13; j++) {
                    //cout<<i*13+j<<"\n";

                    int position=i*13+j; //from 0 to 51

                    cout<<"'"+value_string[j]+"'"<<" <-> ";

                    Card card=*(deck.at(position));
                    cout<<card.toString()<<" ";

                        if(card.value == j){
                            removed_count++;
                            deck.erase(deck.begin()+position); //problems here!
                            cout<<"removed\n";
                        }else{
                            cout<<"not removed\n";
                        }

                    }               
            }
            if(deck.empty()) break;
        }while(removed_count>0);

        if(deck.empty()){
            cout<<"You win!\n";
        }else{
            cout<<"You lose!\n";
        }

        return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:37:26+00:00Added an answer on June 11, 2026 at 11:37 pm

    Well, when I launch it, I get

    'KING' <-> terminate called after throwing an instance of 'std::out_of_range'
      what():  vector::_M_range_check
    

    Obviously that means you are trying to access something beyond the vector bounds. You are erasing an element in the cycle:

    deck.erase(deck.begin()+position);
    

    After this hits, the size of the vector will be 51 instead of 52. However, your cycles are handling 52 values, as you clearly mention in this comment:

    int position=i*13+j; //from 0 to 51
    

    So after the item is removed and there are 51 items left in the deck (0 to 50), the last iteration will still try to access the 51st element and crash. The actual line where it fails is this one:

    Card card=*(deck.at(position));
    

    when the state is the following:
    enter image description here

    As you can see, there are 51 elements in the vector, and *(deck.at(position)) will try to access the 52nd one (which index is 51 since you count from zero), and throw the exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this very simple custom component, made out of two Labels : _left
I made this very simple code for button clicks. Clicking one button changes it's
So I made this (very simple) program with a swing GUI with NetBeans, and
This one very simple thing I can't find the right technique. What I want
I made a very very simple small app to take screenshot of the desktop
I made this very simple 3D model exported to .x format with Google SketchUp,
I am puzzled by this: I have made a very simple example: MainWindow.xaml: <Window
I made a very simple test application with one activity and one layout. The
I made a research and all posts here are very blury regarding this issue.
I made this function to verify a user's twitter credentials. Its running on two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.