Sunday, August 2, 2009

Question in C?

Program keeps crashing any suggestions????





int craps()


{


int roll = pairOfDice();


int flag = 0;


int bet;


char count;


int dummy;








//prompt user for bet


printf("How many chips would you like to bet?");


scanf("%d", %26amp;bet);


printf("\n");





if(bet %26lt; getChips())// player has enough chips?


{


if(bet != 0)


{


setChips(getChips() - bet);


}


}











printf("Press 'r' and hit enter for your first roll.\n");


scanf("%d%c", dummy, count);





if(count = 'r')


{


printf("You rolled a %d. \n", roll);


if(roll == 7)


return 1;


else if(roll == 11)


return 1;


else


k +=roll; // adds roll to counter


}














while(flag == 0)


{





int count1 = 0;


printf("Press 'r' and hit enter for your first roll.\n");


scanf("%c", count1);


if(count1 == 1)


{


roll = pairOfDice();


printf("You r

Question in C?
This line,





scanf("%d%c", dummy, count);





change to


count = getchar();


flush(stdin);





Anywhere you need a single character input


use getchar() with fflush(stdin) after it to remove excess input.


getchar() requires the user to press enter after input.


Alternatively, if you include conio.h


getch() will get your user input. This function will return any key pressed by user. If user presses function keys and arrow keys, getch() will return 0 then scan code on next call.
Reply:scanf("%d%c", dummy, count); would cause the problem.





scanf("%d%c", %26amp;dummy, %26amp;count); will stop crash, but it won't fix the program.





You must pass the address of the integer or address of the character to the scanf function if you expect scanf to be able to change the value of the integer or character.
Reply:hmm, is this your homework...


No comments:

Post a Comment