Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct card {
- char suit[10];
- int value;
- };
- int main(void) {
- //Create an array of a deck of type card
- struct card deck[52];
- //Loop to create the elements of the deck array
- //Two main loops,
- //one for the 13 cards in a suit,
- //Another for the 4 suits.
- int i, j, k = 1;
- int num = 0;
- for(i = 1; i <= 4; i++) {
- for(j = 1; j <= 13; j++) {
- deck[k].value = j;
- switch(i) {
- case 1:
- strcpy(deck[k].suit, "hearts");
- break;
- case 2:
- strcpy(deck[k].suit, "diamonds");
- break;
- case 3:
- strcpy(deck[k].suit, "clubs");
- break;
- case 4:
- strcpy(deck[k].suit, "spades");
- break;
- default:
- strcpy(deck[k].suit, "hearts");
- break;
- }
- k++;
- }
- }
- //Debug
- //Testing, print out all cards.
- /*
- for (i = 0; i < 52; i++) {
- printf("Card %d is %d of %s\n", i, deck[i].value, deck[i].suit);
- }
- */
- //Prompt keyboard input of a number from 1 to 13.
- printf("Please input a number from 1 to 13: \n");
- scanf("%d", &num);
- //Debug print out the input number.
- printf("Number chosen was: %d\n", num);
- //For loop to iterate through entire deck of cards
- //and print whether a certain value is found.
- for(i = 0; i < 52; i++) {
- if(deck[i].value == num) {
- printf("Card: %d is %d of %s\n", i, deck[i].value, deck[i].suit);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement