Im using scanf to accept one character every so often however since both scanfs are in a loop even when entering just one character i get the scanf function returned again except one of them is blank and the other is waiting for input to give a better idea on what I am talking about here is the function its in.
Code:
void game(char *playerhand[10], char *dealerhand[10])
{
int cardsUsed;
char card[80], hit, playagain;
/* Shuffle the Deck */
shuffleDeck(deck);
while (1) /* Allow One Game */
{
/* Allocate 1 char of space for NULL Character for player hands*/
printf("Allocating Memory\n");
handMemAllocate(playerhand);
handMemAllocate(dealerhand);
printf("Memory Allocated\n");
/* Start Dealing Game */
printf("Dealer is Dealing\n");
cardsUsed = startDeal(playerhand,dealerhand);
/* Players turn */
while (1)
{
layout(playerhand, dealerhand, cardsUsed);
printf("\nChecking Players Hand Value\n");
if (handvalue(playerhand, cards(playerhand)) > 21 || handvalue(playerhand, cards(playerhand)) == 21)
{
printf("Players Hand Value Checked and == 21 or > 21\n");
break;
}
printf("\n(H)it or (S)tand: ");
scanf("%c",&hit);
if (hit == 'h' || hit == 'H')
{
printf("Hit getting card\n");
cardsUsed = nextCard(deck, suit, face, card);
playerhand[cards(playerhand)] = (char *)realloc(playerhand[cards(playerhand)],strlen(card)+1);
strcpy(playerhand[cards(playerhand)],card);
printf("Card Got and copied to player\n");
}
if (hit == 's' || hit == 'S')
break;
}
printf("Doing Dealers Hand\n");
while (handvalue(dealerhand, cards(dealerhand)) <= 17)
{
printf("Getting Dealer a Card\n");
cardsUsed = nextCard(deck, suit, face, card);
dealerhand[cards(dealerhand)] = (char *)realloc(dealerhand[cards(dealerhand)],strlen(card)+1);
strcpy(dealerhand[cards(dealerhand)],card);
printf("Dealer got a card\n");
}
layout(playerhand, dealerhand, cardsUsed);
/* Get Winner */
/* Check User wants to play again */
printf("\nChecking if whating to play again\n");
while (1)
{
printf("\nPlay Again (y) or (n): ");
scanf("%c",&playagain);
if (playagain == 'n' || playagain == 'N' || playagain == 'y' || playagain == 'Y')
break;
}
if (playagain == 'n' || playagain == 'N')
break;
}
}
Now is there some way i can empty the buffer before requiring scanf again? is there a better method for getting only a single character? I used a function which attempted to clear the buffer however every so often itd ask for input to continue.
Ive also looked at getchar however the same thing happens there too.