C++ pull a card from deck

Viewed 493

I'm trying to create a program that will pull a card from a deck of 52 regular playing cards.

  • Suits: Heart, Spad, Diamond, Club.

  • Rank: A,2,3,4,5,6,7,8,9,10,J,Q,K.

This should be the output:

Let's pull a card!
This time we got AH
Wanna pull a card again?
y
This time we got 3J
Wanna pull a card again?
n

My output is:

Let's pull a card!
DKThis time we got 00
Wanna pull a card again?
n

This is my code:

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

// Function Declaration
int rankCard(), suitCard();

int main()
{
    srand(time(0));

    char answer;

    cout << "Let's pull a card!" << endl;

    do {
        cout << "This time we got " << rankCard() << suitCard() << endl;
        cout << "Wanna pull a card again?" << endl;
        cin >> answer;
    } while ((answer == 'y') || (answer == 'Y'));

    return 0;
}

int rankCard() {
    int rank = (rand() % 13) + 1;
    switch (rank) {
    case 1: cout << "A";
        break;
    case 10: cout << "T";
        break;
    case 11: cout << "J";
        break;
    case 12: cout << "Q";
        break;
    case 13: cout << "K";
        break;
    default: cout << rank;
        break;
    }

    return 0;
}

int suitCard() {
    int suit = (rand() % 4) + 1;
    switch (suit) {
    case 1: cout << "H";
        break;
    case 2: cout << "D";
        break;
    case 3: cout << "C";
        break;
    case 4: cout << "S";
        break;
    }
    return 0;
}

I can't figure out why the cards pulled (DK) are in that position and why I also get the 00. What am I doing wrong? Thanks

2 Answers

Your calls to rankCard() and suitCard() always return 0.

That 0 value is what's passed to cout in your main function.

The weird 'DK' is caused by the calls to cout inside rankCard and suitCard.

You could modify your functions to avoid the confusion:

#include <string>

std::string rankCard() {
    int rank = (rand() % 13) + 1;
    switch (rank) {
       case 1: return "A";
       case 10: return "T";
       case 11: return "J";
       case 12: return "Q";
       case 13: return "K";
       default: return std::to_string( rank );
    }
    return "";
}
std::string suitCard() {
    int suit = (rand() % 4) + 1;
    switch (suit) {
        case 1: return "H";
        case 2: return "D";
        case 3: return "C";
        case 4: return "S";
    }
    return "";
}

This line:

cout << "This time we got " << rankCard() << suitCard() << endl;

So those functions print the card, and then they return 0, so if you call them in cout, they will do their thing witch is printing the card and then print the return value witch is 0.

What you can do is to call them outside the cout, just do:

//...
cout << "This time we got ";
rankCard();
suitCard(); 
cout << endl;
cout << "Wanna pull a card again?" << endl;
//...

Personally I would refactor the functions to return the respective card char:

Live sample

const char rankCard() {
    int rank = (rand() % 13) + 1;
    switch (rank) {
    case 1: return 'A';
    case 10: return 'T';
    case 11: return 'J';
    case 12: return 'Q';
    case 13: return 'K';
    default: return rank + 48; // convert to decimal digit
    }   
}

const char suitCard() {
    int suit = (rand() % 4) + 1;
    switch (suit) {
    case 1: return 'H';
    case 2: return 'D';
    case 3: return 'C';
    case 4: return 'S';
    default: return 0;  //ASCII code for null character
    }
}
Related