Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#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 14: 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;
};

//ostream & operator<<(ostream &, Player&);

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\t Col 2 \t\t Col 3 \t\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;
    player.draw(deck);
    //while (!deck.isEmpty())
    //{
    cout << "Enter a column number (0 to draw four new cards): " << endl;
    //}
    player.print();
    
    int input;
    int i;
    vector<vector<card> > columns(4);
    while (cin >> input)
        if (input == 0 )
        {
            player.draw(deck);
            player.print();
            columns.push_back(vector<card>());
            columns.push_back(vector<card>());
            columns.push_back(vector<card>());
            columns.push_back(vector<card>());
        }
        else while (cin >> input)
            if (input == 1)
            {
                for ( i = 0; i > 4; i++)
                {
                    columns.push_back(vector<card>());
                }
                for ( i = 0; i > 4; i++)
                {
                    columns[0].back().getSuit();
                    columns[1].back().getSuit();
                    columns[2].back().getSuit();
                    columns[3].back().getSuit();
                }
             
            }
    
}


What I have tried:

i have tried to put return 0; at the end of my int main() but I still get the same warning
Posted
Updated 11-Dec-18 12:44pm
v2
Comments
Rick York 11-Dec-18 18:48pm    
One comment on your coding style: I think you should put the braces around statements that don't necessarily need them. I was thinking primarily about the while loop in your main function. I would write it like this:
    while (cin >> input)
    {
        if (input == 0 )
        {
            player.draw(deck);
            player.print();
            columns.push_back(vector<card>());
            columns.push_back(vector<card>());
            columns.push_back(vector<card>());
            columns.push_back(vector<card>());
        }
        else while (cin >> input)
        {
            if (input == 1)
            {
                for ( i = 0; i > 4; i++)
                {
                    columns.push_back(vector<card>());
                }
                for ( i = 0; i > 4; i++)
                {
                    columns[0].back().getSuit();
                    columns[1].back().getSuit();
                    columns[2].back().getSuit();
                    columns[3].back().getSuit();
                }
            }
        }
    }
Patrice T 11-Dec-18 20:10pm    
The compiler message also tells you where is the problem.
Do you plan to tell us too ?

Such a warning should be the least of your worries: you code produces many errors, starting from line 133:
Quote:
typedef vector<card> cards;
where "‘card’ was not declared in this scope".

You should first fix compiler errors and then focus on the warnings.
 
Share this answer
 
You have two non-void functions that are not returning anything. The first is Card Player::draw(Deck & aDeck) which should return a Card but it does not. The second is main which should return an integer.
 
Share this answer
 
Comments
Member 14090419 15-Dec-18 5:58am    
Well, technically main does not really require a return value, the standard specifies that the compiler will insert return 0; at the end of main if you don't do so yourself.
Check your compiler output - it should be telling you which line the error is on something like:
example.cpp:42 warning: control reaches end of non void function.

But when I try to compile this, I got about 50 errors and warnings, so you might want to take a close look at both code as posted and your compiler output.

NB. Thanks for re-posting the code with indents - though 200+ lines of code are usually not well received.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900