Deck Of Cards - No Output

So, I'm still new at C++, so my syntax might be a little off for getting the correct output. Currently, there are no compiler errors or warnings, but there is also no output when I run the program. It seems as if it is not calling something correctly maybe the .toString or perhaps my DeckOfCards constructor is a little off, either way, I can't seem to figure out why there is no output.

Anyways, here is my code, let me know if you see anything that would remedy this problem:

Card.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#ifndef CARD_H
#define CARD_H
#include <string>
using namespace std;

class Card
{
	public:
		static const int totalFaces = 13; // total number of faces
		static const int totalSuits = 4; // total number of suits
		Card( int cardFace = 1, int cardSuit = 1 ); // initialize face and suit
		string toString() const; // returns a string representation of a Card

		// get the card's face
		int getFace() const
		{
			return face;
		} // end function getFace
		
		// get the card's suit
		int getSuit() const
		{
			return suit;
		} // end function getSuit

	private:
		int face;
		int suit;
		static const string faceNames[ totalFaces ];
		static const string suitNames[ totalSuits ];
}; // end class Card
#endif 


Card.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include "Card.h"
#include "DeckOfCards.h"
using namespace std;

const string Card::faceNames[ totalFaces ] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
const string Card::suitNames[ totalSuits ] = {"Hearts", "Clubs", "Diamonds", "Spades"};

Card::Card( int cardFace, int cardSuit )
{
	face = cardFace;
	suit = cardSuit;
}

string Card::toString() const
{
	return faceNames[ face ] + " of " + suitNames[ suit ];
}


DeckOfCards.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef DECK_OF_CARDS_H
#define DECK_OF_CARDS_H
#include <vector>
#include "Card.h"
using namespace std;

// DeckOfCards class definition
class DeckOfCards
{
	public:
		DeckOfCards(); // constructor initializes deck
		void shuffle(); // shuffles cards in deck
		Card dealCard(); // deals cards in deck
		bool moreCards() const; // are there any more cards left
	
	private:
		vector< Card > deck; // represents deck of cards
		unsigned currentCard; // index of next card to be dealt
}; // end class DeckOfCards
#endif 


DeckOfCards.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "DeckOfCards.h"
#include "Card.h"
using namespace std;

// DeckOfCards default constructor initialized deck
DeckOfCards::DeckOfCards()
{
	// create a new deck
	vector <Card> newDeck;

	// populate the deck
    for (int i = 0; i < Card::totalFaces; i++)
    {
        for (int j = 0; j < Card::totalSuits; j++)
        {
            newDeck.push_back(Card(i,j));
            currentCard++;
        }
    } 

	// assign the new deck to the deck vector
	deck = newDeck;
}

void DeckOfCards::shuffle() // shuffles cards in deck
{
	for (int i = 0; i < 52; i++)
	{
		int randCard = rand() % 52;

		// swaps card with random card
		Card swap = deck[i];
		deck[i] = deck[randCard];
		deck[randCard] = swap;
	}
}

Card DeckOfCards::dealCard() // deals cards in deck
{
	return deck[currentCard++];

} 

bool DeckOfCards::moreCards() const // checks if the current card is out of bounds, if so, no more cards
{
	if(currentCard<=52)
		return true;
	else
		return false;
}


Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <iomanip>
#include "DeckOfCards.h" // DeckOfCards class definition
using namespace std;

int main()
{
	DeckOfCards myDeckOfCards;
	myDeckOfCards.shuffle(); // place Cards in random order

	// print all 52 Cards in the order in which they are dealt
	for ( int i = 1; myDeckOfCards.moreCards(); ++i )
	{
		// deal and display a Card
		cout << left << setw( 19 ) << myDeckOfCards.dealCard().toString();
		if ( i % 4 == 0 ) // output newline every 4 cards
			cout << endl;
	} // end for
} // end main 
currentCard will be 53 after the constructor runs, so moreCards will return false and the for loop will never run.

EDIT: It will actually be random, but most likely above 52
Last edited on
Thank you! I fixed it. Appreciate your help!
Last edited on
Can you please explain how you fixed this? I am not seeing where the error is.
Topic archived. No new replies allowed.