Thread: C language help!cards drawing

  1. #1
    Registered User
    Join Date
    Jun 2014
    Posts
    1

    C language help!cards drawing

    I am trying to make this program where the user input the number of cards they wanna draw and the computer displays the suit and the card. I have used random number, a switch statement for the special case of Ace, Queen, King and Jack, and a for() loop for the number of cards desired. i have also used a check that the input is not anything other than 0-52. the problem that I have is the repetition of cards, and i want to know if there is a way to draw from only the leftover cards. please help ASAP. Thank you!

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    int cardcheck(int x);
    card(int y, int z);
    main(){
        int a,b,c,d,e,f,g,h[52],i;    
        printf("Say how many cards do you wanna draw?    ");
        scanf("%d",&c);
        cardcheck(c);
        srand(time(NULL));
        for(d=0;d<c;d++){
            a= 3+(rand()%4);
            b= 1+(rand()%13);            
            card(a,b);    
            }
    
    
    }
    cardcheck(int x){
        if(x>=0 && x<=52)
        printf(" \b");
        
        else{
        printf("Invalid input");
        exit(0);
        }
    }
    
    
    card(int y,int z){
        switch (z){
        
                case(1):
                    printf("The card you drew is a A of %c\n",y);
                    break;
            
                case(11):
                    printf("The card you drew is a Jack of %c\n",y);
                    break;
            
                case(12):
                    printf("The card you drew is a Queen of %c\n",y);
                    break;
            
                case(13):
                    printf("The card you drew is a King of %c\n",y);
                    break;
            
                default:
                    printf("The card you drew is a %d of %c\n",z,y);
                    break;        
                }
    
    
    }

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    I'll give you a way to approach this problem (just the first thing that popped into my head, I'm sure there are many better ways).

    Have a structure that represents a "card". The struct can have an attribute that represents a Spade , Diamond, Club, Heart and a number (signifying) Ace-King, along with some sort of boolean flag (this will indicate if it has been chosen yet).

    Then when the card is "chosen" you can set the flag to equal true, meaning it cannot be drawn again.

    Let me know if this helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sim Cards
    By Cameronsmith63 in forum C Programming
    Replies: 2
    Last Post: 07-22-2011, 02:28 AM
  2. What's the Difference Between a Programming Language and a Scripting Language?
    By Krak in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 07-15-2005, 04:46 PM
  3. Cards
    By Zoalord in forum Windows Programming
    Replies: 2
    Last Post: 04-11-2004, 01:41 AM
  4. cards.dll
    By bonkey in forum Windows Programming
    Replies: 0
    Last Post: 10-28-2002, 03:21 PM
  5. video cards
    By lambs4 in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 04-09-2002, 10:54 AM

Tags for this Thread