Thread: scanf issue - character in buffer

  1. #1
    Registered User
    Join Date
    Jan 2010

    scanf issue - character in buffer

    basically the program is just a guessing game. you guess a number between 1 and 1000. my friends run it and it works, so i think its a linux problem. when the program asks if i want to play again, it just kills the program unless i scan in two letters, when all im looking for is a 'y' or an 'n'. does anybody know why this happens? i run ubuntu

    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/time.h>
    int readGuess()
    	int answer;
    	scanf( "%d", &answer );
    	return answer;
    /*returns initial guess*/
    int initGame(int *random)
    	*random = (rand()%1000) + 1;
    	printf("I have a number between 1 and 1000\n");
    	printf("Can you guess my number\n");
    	printf("Please type your first guess. ");
    	return readGuess();
    int guessAgain()
    	return readGuess();
    int main()
    	/*randomize number to guess.*/
    	srand((unsigned) time (NULL));
    	char cont = 'y';
    	int random;
    	int answer = initGame(&random);
    	while (cont == 'y')
    		if (answer == random)
    			printf("Excellent! You guessed the number!\n");
    			printf(" Would you like to play again (y or n)? ");
    			fflush (stdin);
    			scanf( "%c", &cont );
    			printf( "DD%cDD", cont );
    			if (cont == 'y')
    				answer = initGame(&random);
    		else if (answer <= random)
    			printf("Too low. Try again.\n");
    		else if (answer >= random)
    			printf("Too high. Try again.\n");
    		answer = guessAgain();

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault
    NEVER USE fflush(stdin)!!!!!!!!!!!!!!!!!

    It is undefined, which means it may or may not work. When it does work, this DOES NOT mean it the proper way to code, since it could still also not work.

    Please read this for alternatives:
    STDIN pitfalls

    Until you do that, you are running non-standard code and there's no point trying to fix it.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jan 2010
    . thanks for that knowledge. i just started to use c and c++. this forum (which i found after i posted this) said the same thing:
    A problem with scanf()? - Linux Forums

    i did comment out fflush, and putting getchar after the scanf that reads in the guess worked.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. about wide character and multiple byte character
    By George2 in forum C Programming
    Replies: 3
    Last Post: 05-22-2006, 08:11 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. buffer contents swapping
    By daluu in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2004, 02:34 PM
  4. Buffering??
    By Brain Cell in forum C Programming
    Replies: 15
    Last Post: 09-21-2004, 06:57 AM
  5. Tetris Questions
    By KneeGrow in forum Game Programming
    Replies: 19
    Last Post: 10-28-2003, 11:12 PM