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

  • Home
  • SEARCH
  • 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 3932518
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:28:04+00:00 2026-05-19T23:28:04+00:00

The program I’ve written creates a deck of card that can be shuffled and

  • 0

The program I’ve written creates a deck of card that can be shuffled and player that takes four cards and puts them into for corresponding columns.

I can display a single card by doing this:

int main()
{
    Card card;
    cout << card << endl;
}

The problem is, I want to display my player class, which is made up of a vector of cards and should display four cards and put them into four separate columns. But using this:

int main()
{
    Deck deck;
    deck.shuffle();

    Player player;
    cout << player;
}

does not display anything, in fact, it gives me an error.

How can I properly display my four cards in their corresponding four columns?

Also, here is the entire code I have so far, in case you want to go through it yourself:

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;

enum suits 
{
    diamond, club, heart, spade
};

class Card
{
private:
    int rank;
    suits suit;
public:
    Card();
    Card(suits, int);
    int getRank() { return rank; }
    suits getSuit() { return suit; }
    void setRank(int rankvalue) { rank = rankvalue; }
    void setSuit(suits suitvalue) { suit = suitvalue; }
};

ostream & operator<<(ostream &, Card);

Card::Card()
{
    rank = 1;
    suit = spade;
}

Card::Card(suits suitvalue, int rankvalue)
{
    rank = rankvalue;
    suit = suitvalue;
}

ostream & operator<<(ostream & out, Card aCard)
{
    switch (int rank = aCard.getRank())
    {
        case 1: out << "Ace"; break;
        case 11: out << "Jack"; break;
        case 12: out << "Queen"; break;
        case 13: out << "King"; break;
        default: out << rank;
    }

    switch (suits suit = aCard.getSuit())
    {
        case diamond: out << " of Diamonds"; break;
        case spade: out << " of Spades"; break;
        case heart: out << " of Hearts"; break;
        case club: out << " of Clubs"; break;
    }

    return out;
}

class RandomInteger 
{
public: 
    RandomInteger();
    unsigned int operator() (unsigned int max);
};

RandomInteger::RandomInteger()
{
    srand(time(0));
}

unsigned int RandomInteger::operator()(unsigned int max)

{
    unsigned int rval = rand();
    return rval % max;
}

RandomInteger randomizer;

class Deck
{
    Card cards[52];
    int topCard;
public:
    Deck();
    void shuffle();
    bool isEmpty() { return topCard <= 0; }
    Card draw();
};

extern RandomInteger randomizer;

Deck::Deck()
{
    topCard = 0;
    for (int i = 1; i <= 13; i++)
    {
        Card c1(diamond, i), c2(spade, i), c3(heart, i), c4(club, i);
        cards[topCard++] = c1;
        cards[topCard++] = c2;
        cards[topCard++] = c3;
        cards[topCard++] = c4;
    }
}

Card Deck::draw()
{
    if (!isEmpty())
        return cards[--topCard];
    else
    {
        Card spadeAce(spade, 1);
        return spadeAce;
    }
}

void Deck::shuffle()
{
    random_shuffle(cards, cards+52, randomizer);
}

class Player
{
public:
    Player();
    void print();
    Card draw(Deck &);
    typedef vector<Card> cards;
    vector<cards> column;
};

Player::Player()
{
    column.push_back(vector<Card>());
    column.push_back(vector<Card>());
    column.push_back(vector<Card>());
    column.push_back(vector<Card>());
}

Card Player::draw(Deck & aDeck)
{
    for (int i = 0; i < 4; i++)
        column[i].push_back(aDeck.draw());
}

void Player::print()
{
    cout << "Col 1 \t Col 2 \t Col 3 \t Col 4 \n";
    bool more = true;
    for (int j = 0; more; j++)
    {
        more = false;
        for (int i = 0; i < 4; i++)
            if (j < column[i].size())
        {
        cout << column[i][j] << "\t";
        more = true;
        }
            else
            cout << "\t\t";
    cout << endl;
    }
}

int main()
{
    Deck deck;
    deck.shuffle();

    Player player;
    while (!deck.isEmpty());
    cout << player;
}

Sorry for the amateur question, I know it’s something simple, and thanks in advance.

  • 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-05-19T23:28:05+00:00Added an answer on May 19, 2026 at 11:28 pm
    cout << player;
    

    This would not work, because you’ve not overloaded operator<< for type Player. Please first implement this:

    ostream & operator<<(ostream &out, const Player & player)
    {
        //your implementation
    }
    

    If it needs to access private members, then make this friend of Player.


    I just noticed that your class Player has a function called print, maybe you would like to call it from operator<<(). If you call it, then I would suggest you to use the following function signature:

       void print(ostream &out) const 
       {
            //use `out`, instead of `cout` now!
       }
    

    And then call this from operator<<, as:

    ostream & operator<<(ostream &out, const Player & player)
    {
        player.print(out);
        return out; 
    }
    

    Advantage of this implementation is that you can output to console as well as file also.

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

Sidebar

Related Questions

What program can I use to decompile a class file? Will I actually get
The program that I am currently assigned to has a requirement that I copy
A program that I work on assumes that the UUID generated by the Windows
My program has to read files that use various encodings. They may be ANSI,
Program A, is a c program that endlessly, receives input in stdin, process it
/* Program that reads a sequence of words from keyboard and prints the list
Program followed by output. Someone please explain to me why 10,000,000 milliseconds from Jan
Most program languages have some kind of exception handling; some languages have return codes,
My program generates relatively simple PDF documents on request, but I'm having trouble with
A program receives a list of Messages (base type). Each message in the list

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.