Print deck of cards in C++

In this experiment, we will learn how to print the deck of cards in C++ language.

#include<iostream>
#include<string>
using namespace std;
int main(){
    std::string cards[13]={"A","J","K","1","2","3","4","5","6","7","8","9","10"};//initializing cards availabel under one sign
std::string sign[4] = { "heart", "diamond","ace", "spade" };//total number of signs
 
for(int i=0;i<4;i++){
    for(int j=0;j<13;j++)
    {
      std::cout<<sign[i];
      std::cout<<cards[j]<<endl;
}
}
}

Code Explanation

  1. In the above code first, we initialize all the necessary libraries including the “string” library since we will use some of the string functions.
  2. Then we declared an array of strings.
  3. The first array is “cards” which have values of all the cards present in one sign which are 13 in total so its size is 13.
  4. The second array is “signs” which have total signs present in one deck of 52 cars which is 4 so its size is 4.
  5. Then we have used nested for loops to print the cards that are their signature and values.
  6. Outer loop will run 4 times and the inner loop will run 13 times for every single run of the outer loop(total of 13*4=52).
  7. So for every single run of the outer loop all 13 cards will be printed as output under one sign.

OUTPUT

heartA
heartJ
heartK
heart1
heart2
heart3
heart4
heart5
heart6
heart7
heart8
heart9
heart10
diamondA
diamondJ
diamondK
diamond1
diamond2
diamond3
diamond4
diamond5
diamond6
diamond7
diamond8
diamond9
diamond10
aceA
aceJ
aceK
ace1
ace2
ace3
ace4
ace5
ace6
ace7
ace8
ace9
ace10
spadeA
spadeJ
spadeK
spade1
spade2
spade3
spade4
spade5
spade6
spade7
spade8
spade9
spade10

OUTPUT

we are getting a total of 52 cards as our output 13 under the same sign.

Leave a Reply

Your email address will not be published.