-
scanf problem
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.
-
Why are you using scanf for just one character? There are better functions for that. getchar comes to mind. At any rate, you're pressing enter, and that's being stored also. How about clearing out the remaining input?
Code:
void clearinput( void )
{
int c;
while( (c = getchar()) != '\n' );
}
Now just use this after your input call. Something like:
Code:
scanf( "%c", &foo );
clearinput();
Now all we're doing is discarding everything up to and including the newline.
There are various ways to handle this sort of thing. And just in case it's crossed your mind, fflush is not one of them.
Quzah.
-
Actually fflush did cross my mind however in the end I didnt use it.
I also have a small question also why is that whenever i see a void argument its spaced out I saw the same in my book as well as your code just there however I dont do it. Is it just a habit? or is there something I am missing?
-
It makes the program run faster if it's spaced out.
No really, it's just style preference.