Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2018
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.40 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct card {
  5.     char suit[10];
  6.     int value;
  7. };
  8.  
  9. int main(void) {
  10.     //Create an array of a deck of type card
  11.     struct card deck[52];
  12.  
  13.     //Loop to create the elements of the deck array
  14.     //Two main loops,
  15.     //one for the 13 cards in a suit,
  16.     //Another for the 4 suits.
  17.  
  18.     int i, j, k = 1;
  19.     int num = 0;
  20.  
  21.     for(i = 1; i <= 4; i++) {
  22.  
  23.         for(j = 1; j <= 13; j++) {
  24.             deck[k].value = j;
  25.  
  26.             switch(i) {
  27.                 case 1:
  28.                     strcpy(deck[k].suit, "hearts");
  29.                     break;
  30.  
  31.                 case 2:
  32.                     strcpy(deck[k].suit, "diamonds");
  33.                     break;
  34.  
  35.                 case 3:
  36.                     strcpy(deck[k].suit, "clubs");
  37.                     break;
  38.  
  39.                 case 4:
  40.                     strcpy(deck[k].suit, "spades");
  41.                     break;
  42.  
  43.                 default:
  44.                     strcpy(deck[k].suit, "hearts");
  45.                     break;
  46.             }
  47.  
  48.             k++;
  49.         }
  50.  
  51.     }
  52.  
  53.     //Debug
  54.     //Testing, print out all cards.
  55.     /*
  56.     for (i = 0; i < 52; i++) {
  57.         printf("Card %d is %d of %s\n", i, deck[i].value, deck[i].suit);
  58.     }
  59.     */
  60.  
  61.     //Prompt keyboard input of a number from 1 to 13.
  62.     printf("Please input a number from 1 to 13: \n");
  63.     scanf("%d", &num);
  64.  
  65.     //Debug print out the input number.
  66.     printf("Number chosen was: %d\n", num);
  67.  
  68.     //For loop to iterate through entire deck of cards
  69.     //and print whether a certain value is found.
  70.     for(i = 0; i < 52; i++) {
  71.         if(deck[i].value == num) {
  72.             printf("Card: %d is %d of %s\n", i, deck[i].value, deck[i].suit);
  73.         }
  74.     }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement