#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    1

    Scanf not working


    I am trying to create the game of craps in C. I have provided here only one function. There is another function called play_game(which I haven't included here) which returns true or false to the main function. Here is the main function(I have commented out the problem):
    Code:
    int main(void)
    {
    	int win = 0, loss = 0;
    	char c;
    		
    	printf("\nWelcome to the game of craps! \nPress y to roll dice: ");
    	scanf("%c", &c);
    	if (c == 'y') {
    		for (;;) {
    			if (play_game()) {
    				win++;
    				printf("\nYou won! Press y to play again. Press any other key to display results: ");
    				scanf("%c", &c); /*This scanf doesn't seem to take my input. It just goes on to the next line while I debug*/
    				if (c != 'y') {
    					break;
    				}
    			}
    			else {
    				loss++;
    				printf("\nYou lose! Press y to play again. Press any other key to display results: ");
    				scanf("%c", &c); /*Neither does this scanf*/
    				if (c != 'y') {
    					break;
    				}
    			}
    		}
    	}
    	if (c != 'y') {
    		printf("\nWins: %d Losses: %d", win, loss);
    	}
    	
    	return 0;
    }
    Look at the comments to see where scanf is not working. Please help me solve the problem.
    Last edited by arman.khandaker; October 26th, 2013 at 10:14 AM.
  2. #2
  3. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,905
    Rep Power
    3969
    Your second scanf call is going to end up reading the new-line character from when you pressed enter after the first input. You need to make sure you skip over those whitespace characters and read the next non-whitespace input. You can do this by adding a space before your %c marker:

    Code:
    printf("\nYou won! Press y to play again. Press any other key to display results: ");
    scanf(" %c", &c); /* leading space will eat the newlines/spaces from previous input. */

    Comments on this post

    • arman.khandaker agrees
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    1
    Originally Posted by kicken
    Your second scanf call is going to end up reading the new-line character from when you pressed enter after the first input. You need to make sure you skip over those whitespace characters and read the next non-whitespace input. You can do this by adding a space before your %c marker:

    Code:
    printf("\nYou won! Press y to play again. Press any other key to display results: ");
    scanf(" %c", &c); /* leading space will eat the newlines/spaces from previous input. */
    Omg, this worked perfectly! Thanks a lot sir! :)
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,073
    Rep Power
    1802
    Originally Posted by kicken
    You can do this by adding a space before your %c marker:
    But that method only consumes whitespace. If the user enters non-white space characters before the newline, that may also cause undesirable behaviour. A better method is:

    C Code:
    char getInChar()
    {
        char ch ;
        scanf( "%c", &ch ) ;
        while( ch != '\n' && getchar() !='\n' )
        {
            // do nothing
        }
        return ch ;
    }


    Then:

    C Code:
    c = getInChar() ;
    if( c == 'y' )
       ...

    Comments on this post

    • arman.khandaker agrees
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    1
    Originally Posted by clifford
    But that method only consumes whitespace. If the user enters non-white space characters before the newline, that may also cause undesirable behaviour. A better method is:

    C Code:
    char getInChar()
    {
        char ch ;
        scanf( "%c", &ch ) ;
        while( ch != '\n' && getchar() !='\n' )
        {
            // do nothing
        }
        return ch ;
    }


    Then:

    C Code:
    c = getInChar() ;
    if( c == 'y' )
       ...
    Thank you sir. This is a good way to implement the scanf. :)

IMN logo majestic logo threadwatch logo seochat tools logo