Thread: Ctype.h?? n fflush??

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    2

    Ctype.h?? n fflush??

    Code:
    		
             do{
                ....printf("Continue? (Y/N)");
    		fflush(stdin);
    		cont = toupper(getchar());
    		if ((cont!='Y') || (cont!='N'))	{
    			printf("Invalid input! Continue? (Y/N) ");
    			fflush(stdin);
    			cont = toupper(getchar());
    		}
              } while(cont=='Y');.....
    Im still new at programming. May i know why the loop doesnt work when i type 'y' ?
    i don't get what is the problem with the code above as this code below works perfectly well?
    Code:
    int main()	{
    	char x;
    
    	x=toupper(getchar());
    
    	if (x=='Y')	{
    		printf("print this!\n");
    	}
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    1) Remove the fflush(stdin) lines. Contrary to what some people claim, fflush() does not discard pending input, except with some oddball compilers or libraries. When used on an input stream it gives undefined behaviour.

    2) if ((cont != 'Y') || (cont != 'N')) means your code will report invalid input, for any input. If cont is 'Y' it is not equal to 'N', so invalid input is reported. If cont is 'N', it is not equal to 'Y', so invalid input is reported. If cont is anything else, it is not equal to either 'Y' or 'N', so .....
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by grumpy View Post
    1) Remove the fflush(stdin) lines. Contrary to what some people claim, fflush() does not discard pending input, except with some oddball compilers or libraries. When used on an input stream it gives undefined behaviour.
    "Oddball compilers or libraries" is a bit harsh -- MSVC defines fflush(stdin) as clearing the buffer: fflush

    So I argue that relying on fflush(stdin) is the same as relying on any compiler- or platform-specific feature. Result: code that is not portable, but will work with a particular compiler on a particular platform. Which isn't great, but it's not WRONG so long as you, the code maintainer, are aware of this.

    Just saying that for the sake of saying it. I agree that the OP should remove fflush(stdin) from the code.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2
    @grumpy : Thx for pointing out my mistake >.< i guess im a bit too careless =p
    @smokeyangel : i will keep that in mind.

    btw, if fflush would result in unportable code, my guess would be system("cls") would be unportable as well? As assigning a dummy input can replace fflush(stdin), is there any alternative way for system("cls") then ?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by smokeyangel View Post
    "Oddball compilers or libraries" is a bit harsh -- MSVC defines fflush(stdin) as clearing the buffer: fflush
    Defining fflush(stdin) to be clearing the input buffer first occurred at a time when Microsoft was deliberately avoiding compliance with several standards they did not control - and documenting their non-standard extensions as being standard - as part of a strategy of vendor lockin.

    Microsoft have since made a strategic effort to comply with various standards (and their Visual Studio suite has improved markedly as a result) but fflush(stdin) is one of many legacies from their previous strategy.

    Given that history, describing such features of compilers or libraries as "oddball" is actually quite gentle, given that more robust characterisations are also applicable.



    Quote Originally Posted by rev91 View Post
    btw, if fflush would result in unportable code, my guess would be system("cls") would be unportable as well? As assigning a dummy input can replace fflush(stdin), is there any alternative way for system("cls") then ?
    Yes, it is true that system("cls") does not have a common effect across platforms.

    Apart from the fact that behaviour of any system() call is implementation defined, "cls" is a MS-DOS and windows shell command for clearing a screen, and the C standard does not require that a computer even have a screen in order to run a C program. Unfortunately, that means the notion of clearing a screen is not even meaningful on all platforms. Which means it is not possible to meaningfully define a replacement for system("cls") that works across systems.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ctype.h HELP !!!
    By player1 in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2011, 05:46 PM
  2. Understanding ctype.h
    By yougene in forum C Programming
    Replies: 27
    Last Post: 03-05-2008, 05:27 PM
  3. Problem with ctype and locales
    By cunnus88 in forum C++ Programming
    Replies: 8
    Last Post: 10-31-2007, 02:44 AM
  4. ctype segmentation fault
    By cunnus88 in forum C++ Programming
    Replies: 8
    Last Post: 04-11-2007, 07:15 AM
  5. Ctype.h problem?
    By caroundw5h in forum C Programming
    Replies: 1
    Last Post: 12-26-2003, 08:42 PM