Answer to Question #318724 in C++ for Sheek

Question #318724

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.

1
Expert's answer
2022-03-27T16:05:04-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog