Write a program that simulates picking a card from a deck of 52 cards. Your
program should display the rank (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King) and
suit (Clubs, Diamonds, Hearts, Spades) of the card.
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
string cardName(int card) {
char* rank[] = {"Ace", "2", "3", "4", "5", "6", "7", "8",
"9", "10", "Jack", "Queen", "King"};
char* suits[] = {"Clubs", "Diamonds", "Hearts", "Spades"};
string res = rank[card%13];
res += " of ";
res += suits[card/13];
return res;
}
int main() {
srand(time(nullptr));
int card = rand() % 52;
string card_name = cardName(card);
cout << card_name << endl;
return 0;
}
Comments
Leave a comment