Thread: Segmentation fault with card game

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    Segmentation fault with card game

    Hello folks! For my C programming class we have to make a card game that goes through 2 "decks" of cards and if the "cards" match we put one of those cards in a matching deck and then after the player wants to stop we print all the matched cards. I am having two problems, one is that my printf to continue prints twice after the first time it asks and second I get a segmentation fault when it gets to my for loop that prints the matched things.

    Here is the code:

    Code:
    #include <stdlib.h>
    #include <time.h>
    //For suits 1 is Diamond, 2 is Heart, 3 is Club, and 4 is Spade
    //For cards 1 is Ace, 11 is Jack, 12 is Queen, and 13 is King
    
    int main()  {
            char **card, **suit, **matched_card, **matched_suit;
            int **matched_count, **matched_pass;
            char go='y';
            int card_count=52, suit_count=52, match=52, pass=52, i, j, k, a, b, c, d, e, f, g, h, match_count=0,pass_count=1;
            card=(char**)calloc(sizeof(char**),card_count);
            suit=(char**)calloc(sizeof(char**),suit_count);
            matched_card=(char**)calloc(sizeof(char**),card_count);
            matched_suit=(char**)calloc(sizeof(char**),suit_count);
            matched_count=(int**)calloc(sizeof(int**),match);
            matched_pass=(int**)calloc(sizeof(int**),pass);
            card[0]="Ace";
            card[1]="Two";
            card[2]="Three";
            card[3]="Four";
            card[4]="Five";
            card[5]="Six";
            card[6]="Seven";
            card[7]="Eight";
            card[8]="Nine";
            card[9]="Ten";
            card[10]="Jack";
            card[11]="Queen";
            card[12]="King";
    
            suit[0]="Diamond";
            suit[1]="Heart";
            suit[2]="Club";
            suit[3]="Spade";
            while(!(go=='n'))  {
                    srand(time(NULL));
                    for(i=1;i<=(52-match_count);i++)  {
                            a=rand()%4;
                            b=rand()%13;
                            c=rand()%4;
                            d=rand()%13;
                            if((card[b]==card[d])&&(suit[a]==suit[c]))  {
                                    ++match_count;
                                    for(j=0;j<match_count;j++)  {
                                            if(!((card[b]==matched_card[j])||(suit[a]==matched_suit[j])))  {
                                                    matched_card[match_count-1]=card[b];
                                                    matched_suit[match_count-1]=suit[a];
                                                    matched_count[match_count-1]=match_count;
                                                    matched_pass[match_count-1]=pass_count;
                                                    //printf("%d\n",match_count);
                                                    //printf("Match!\n");
                                            }
                                    }
                            }
                    }
                    printf("Only %d matches left, keep going (y or n): ",(52-match_count));
                    scanf("%c",&go);
                    printf("%c\n",go);
                    ++pass_count;
            }
            printf("Match  Pass     Suit    Card\n");
            for(k=0;k<52;k++)  {                                    //THIS IS THE PROBLEM FOR LOOP
                    //printf("This works\n");
                    printf("%6s",matched_count[k]);
                    printf("%5s",matched_pass[k]);
                    printf("%10s",matched_suit[k]);
                    printf("%8s",matched_card[k]);
                    //printf("%6s%5s%10s%8s\n",matched_count[i],matched_pass[i],matched_suit[i],matched_card[i]);
            }
    
    }
    Ive looked through all my pointers many times and I cant seem to find where I am going over the memory Ive allocated for it.
    Last edited by bwisdom; 11-05-2009 at 04:07 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 05:50 PM
  3. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM
  4. strcat segmentation fault
    By captain-cat in forum C Programming
    Replies: 3
    Last Post: 07-20-2004, 10:29 AM
  5. Segmentation Fault Error
    By jcramer in forum C Programming
    Replies: 2
    Last Post: 11-23-2003, 02:16 PM