Thread: scanf problem

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    68

    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.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    68
    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?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It makes the program run faster if it's spaced out.

    No really, it's just style preference.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with RPC and scanf
    By Ricardo_R5 in forum C Programming
    Replies: 11
    Last Post: 01-08-2007, 06:15 PM
  2. Quick problem aboutf scanf
    By BuezaWebDev in forum C Programming
    Replies: 4
    Last Post: 03-28-2005, 06:01 PM
  3. Problem with scanf float..
    By aydin1 in forum C Programming
    Replies: 6
    Last Post: 03-06-2005, 01:35 PM
  4. problem with looping scanf
    By crag2804 in forum C Programming
    Replies: 6
    Last Post: 09-12-2002, 08:10 PM
  5. scanf problem
    By Flikm in forum C Programming
    Replies: 2
    Last Post: 11-05-2001, 01:48 PM