Thread: do - while loop ignores getchar()

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    2

    do - while loop ignores getchar()

    Hi,
    my problem is pretty much summarized in the title, following code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main ()
    {
      char a;
      int DP;int x=1;int gcount = 0;int rolls = 0;int success = 0;
      
    	do
    	{
      /* initialize random generator */
    	srand ( time(NULL) );
    	printf("Bitte Dicepool eingeben\n");
    	scanf("%d",&DP);
    		while ( x<=DP)
    		{
    		int random = (rand() % 6)+1;
    		printf ("Ergebniss: %d\n",random); 
    		x++;
    			if(random == 1)
    				{gcount++;} 
    			else if(random == 6||random == 5)
    				{success++; 
    				if(random == 6){x--;}}
    		rolls++;
    		}
    	
    	printf("\nErfolge:%d, 1en:%d",success,gcount);
    
    	printf("\n Nochmal (j/n)? \n"); /*asks if the user wants to go again*/
    	a = getchar();
    	}while( a == 'j');
      return 0;
    }
    I'm trying to write a diceroller for shadowrun as a little exercise with C. Now i played around a bit, commenting out stuff and changing conditions, and it seems the program skips the "a=getchar();" line, it immediately stops after asking for another go. I use Geany as an IDE(compiler didn't show any errors) under Ubuntu 10, and when i tried executing the code in Terminal it did exactly the same thing it did when executed directly from Geany.
    I hope this isn't too stupid a question,I'm pretty noobish with C.
    Thanks for the time.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The scanf leaves the newline character in the input buffer/stream, this is then read by the getchar function. You'll need to take care of that leftover character.

    Your srand function should probably be outside the do/while loop, you shouldn't need to repeatedly reseed the random number generator (once is usually fine)... unless that's what you want to happen.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    2
    Thank you very much, I used a getchar(); directly after the scanf() to clear it and it is working now. Is this a feasible solution or should I use something else?

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    initialise your char to 'j' before you start and use a while loop instead maybe.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  2. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  3. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM
  4. getchar() problem from K&R book
    By anemicrose in forum C Programming
    Replies: 13
    Last Post: 04-04-2004, 11:06 PM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM