Why is this crashing? - C -
i close finishing this, @ least hope because if i'm not i'm going go insane. i've managed rid of of syntax , errors rid of compiler, start in hopes done , then... crashes...
to give bit of overview of how it's suppose work, it's suppose print out 5 values 'deck' - randomly shuffle print again (to show shuffled)
i'm close. can taste it!
#include <stdio.h> #include <string.h> #include <stdlib.h> #define size 52 enum faces{ace = 0, jack = 10, queen, king}; char * facecheck(int d); void shuffle( int deck[]); void draw(int deck[size]); void cards(int hand); int main() { int deck[size], i, n; char suits[4][9] = { "hearts", "diamonds", "clubs", "spades"}; srand( time( null ) ) ; for(i = 0; i<size; i++) { deck[i] = i; i++; }; draw(deck); shuffle(deck); draw(deck); return 0; } char * facecheck(int d) { static char * face[] = { "ace", "jack", "queen", "king" }; if(d == ace) return face[0]; else { if(d == jack) return face[1]; else { if(d == queen) return face[2]; else { if(d == king) return face[3]; } } } } void shuffle( int deck[]) { int i, j, temp; for(i = 0; < size; i++) { j = rand() % size; temp = deck[i]; deck[i] = deck[j]; deck[j] = temp; } printf("the deck has been shuffled \n"); } void draw(int deck[size]) { int i; int hand[i]; for(i = 0; < 5; i++) { cards(hand[i]); putchar('\n'); } } void cards(hand) { int i; char suits[4][9] = { "hearts", "diamonds", "clubs", "spades"}; for(i=0; i<size; i++) { if(i%13 == 0 || i%13 == 10 || i%13 == 11 || i%13 == 12) printf("%s ", facecheck(i%13) ); else printf("%d ", i%13+1); printf("of %s \n", suits[i/13]); } }
it crashes because here:
for(i = 0; i<size; i++) { deck[i] = i; i++; }; you increment twice. remove i++ line:
for(i = 0; i<size; i++) { deck[i] = i; }; also, here:
void draw(int deck[size]) { int i; int hand[i]; the line int hand[i] fail because not constant (it's not initialized also).
Comments
Post a Comment