C - Honestly at a loss for this odd behaviour -
so... have little piece of code have been working on of morning. it's little project me remember syntax , on. have missed kind of massive error code returns segmentation fault reasons don't understand.
#include <stdio.h> #include <stdio.h> #include <unistd.h> #include <time.h> #include <getopt.h> struct cards { int card_value[99]; char card_name[99]; char card_suit[99]; int card_tally; }; struct cards hand[2]; void tally (int a) { int k, j; (k=0; k<5; k++) { j = j + hand[a].card_value[k]; } hand[a].card_tally = j; } void check_for_ace (int a) { int d; (d=0; d>5; d++) { if (hand[a].card_name[d] =='a') { int y; int z = 10; (y=0; y<5; y++) z = z + hand[a].card_value[y]; if (z < 22) hand[a].card_value[d]=10; else hand[a].card_value[d]=1; } } } void draw_card (int a) { int z = 1 + rand () % 13; int x=0; while (hand[a].card_value[x] != 0) { x++; } if ((z > 1) && (z < 10)) { hand[a].card_value[x]=z; hand[a].card_name[x]=((char) '0' + z); } else if (z == 10) { hand[a].card_value[x]=z; hand[a].card_name[x]='t'; } else if (z == 11) { hand[a].card_value[x]=10; hand[a].card_name[x]='j'; } else if (z == 12) { hand[a].card_value[x]=10; hand[a].card_name[x]='q'; } else if (z == 13) { hand[a].card_value[x]=10; hand[a].card_name[x]='k'; } else if (z == 1) { /*function 'check_for_ace' deals more properly*/ hand[a].card_value[x]=1; hand[a].card_name[x]='a'; } check_for_ace(a); tally(a); } void display_hands () { int x; printf("\ndealer's hand shows: "); (x=0; hand[0].card_name[x]!=0; x++) { printf("%c ", hand[0].card_name[x]); } printf("\nplayer's hand shows: "); (x=0; hand[1].card_name[x]!=0; x++) { printf("%c ", hand[1].card_name[x]); } } void dealer_turn() { while (hand[0].card_tally < 17) draw_card(0); } void player_turn() { int op=0; while (op != 2) { printf("what do?\n"); printf("(1)it or (2)tand\n"); scanf("%d", &op); if (op == 1) draw_card(1); } } int main(int argc, char **argv) { srand(time(null)); draw_card(0); draw_card(1); draw_card(1); display_hands(); player_turn(); dealer_turn(); display_hands(); return 0; } now, odd thing is, see empty line between player_turn , dealer_turn? if put display_hands in there code executes without error.
any ideas?
also, realize did use headers don't require. plan use them later , left them in post have left error.
you aren't initializing j before execute line.
j = j + hand[a].card_value[k]; when added j=0 initialization code, no longer crashes.
e.g.
void tally (int a) { int k, j; j=0; /* <----- added line */ (k=0; k<5; k++) { j = j + hand[a].card_value[k]; } hand[a].card_tally = j; }
Comments
Post a Comment