Segmentation fault with card game

This is a discussion on Segmentation fault with card game within the C Programming forums, part of the General Programming Boards category; Hello folks! For my C programming class we have to make a card game that goes through 2 "decks" of ...

  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.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Have you tried a debugger?

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    The compiler we use is a server on campus, obviously without a debugger, and I haven't really even thought about setting one up. Ill see if I can't find something.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    It probably does. Most UNIX/Linux installations do.

    Code:
    gcc -g -o asdf asdf.c
    gdb asdf

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    Meh I ended up just coming to campus and using Visual Studio Express. There error I get now is
    Code:
    "cxx0030 error expression cannot be evaluated"
    with each printf in the for loop

    Also, it didnt start doing this until I added this:
    Code:
                            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");
                                            }
                                    }
                            }
    initially I just had it print the matches after each pass, the problem with that was its all supposed to be output at the end, plus it didnt have a check for cards that were already matched.
    Last edited by bwisdom; 11-05-2009 at 05:28 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When you use a scanf(), remember:

    1) scanf() for numbers will skip over the newline char from the enter key, still in the keyboard buffer.

    2) scanf() for char's will *NOT* skip over any newline char from an earlier scanf() call. It will take the newline char AS THE USER'S CHAR, and go on.

    3) To prevent #2, you need to pull the newline char off the keyboard buffer, before using a scanf() for a char, UNLESS it's the first scanf() in the whole program.

    You do that by adding this line of code, before the scanf() for the char:

    Code:
    YourVariable = getchar();
    //your variable can be either an int or a char, either will pull the newline off the keyboard buffer.
    Usually, you want to group a things (cards in your case) characteristics, into a struct, and then use an array of those structs in your program's logic.

    Code:
    struct card {
      int suit;    //in order of strength 1-4 or 0-3
      int value;  // maybe jacks=11, queen's=12, kings=13, aces=14
      int inPlay; //a boolean value if you like inplay=1 or 0
    } card1;
    Add any other members to the struct that you could use for your particular game/program.

    It's so easy to work with things in arrays, I don't see why you're using this half-array, half pointer style of logic.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    I found the problem. I was only allocating the in 1d. I needed to add for loops to allocate in the second dimension. Plus, on the int pointers, I only had to allocate in 1d instead of 2 so I got rid of that.

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, 01:53 PM
  4. strcat segmentation fault
    By captain-cat in forum C Programming
    Replies: 3
    Last Post: 07-20-2004, 11:29 AM
  5. Segmentation Fault Error
    By jcramer in forum C Programming
    Replies: 2
    Last Post: 11-23-2003, 02:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21