Thread: Frustrated with the instablility of scanf() and gets() functions

  1. #16
    Registered User
    Join Date
    Apr 2009
    Posts
    1
    Hi all,

    The easiest way is to add the line

    scanf("%*c");

    This will take a way the end of line in the buffer.

  2. #17
    Registered User
    Join Date
    Apr 2009
    Location
    Malaysia
    Posts
    2
    I'm not a Pro Programmer, but then I could suggest you to use a single line and get rid off your probrlem!!!

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <ctype.h>
    #include <process.h>
    
    #define pause getch()
    #define clear clrscr()
    
    
    void main(void) {
    
    	char UserName[25];
    	char UserPhone[10];
    	char UserChoice = ' ';
    
    	clear;
    
    	printf("\nName: ");
    	gets(UserName);
    
    	printf("\nPhone: ");
    	gets(UserPhone);
    
    	printf("\nContinue [Y/N] ? : ");
    	scanf("%c", &UserChoice);
    	fflush(stdin); // flush the standard input stream after scanf
    
    	if(toupper(UserChoice) == 'Y')
    		main();
    	
    	printf("\nPress any key to exit...");
    	pause;
    	exit(0);
    }
    fflush(stdin) flushes the standard input stream... fflush() function is defined in <conio.h> and is supplied with all the compilers I've used!!

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by musaid
    fflush(stdin) flushes the standard input stream.
    No, it results in undefined behaviour. fflush() is only defined for output and update streams.

    Quote Originally Posted by musaid
    fflush() function is defined in <conio.h>
    No, it is declared in <stdio.h>

    Quote Originally Posted by musaid
    and is supplied with all the compilers I've used!
    That is probably true, since it is part of the C standard library.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by musaid View Post
    I'm not a Pro Programmer, but then I could suggest you to use a single line and get rid off your probrlem!!!

    fflush(stdin) flushes the standard input stream... fflush() function is defined in <conio.h> and is supplied with all the compilers I've used!!
    before suggesting code you should read FAQ

    about gets, flush sdtdin, void main...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #20
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by vohkiem View Post
    The easiest way is to add the line

    scanf("%*c");

    This will take a way the end of line in the buffer.
    what this line actually did.i put it and yes the problem was solved.can you explian it a bit.

    also when i put this line instead of scanf
    Code:
    printf("\nContinue [Y/N] ? : ");
    UserChoice=getchar();
    then also the same problem persisted.why?does getchar also leaves the any character in the buffer?
    Thank You
    Last edited by BEN10; 04-04-2009 at 08:49 AM.

Popular pages Recent additions subscribe to a feed

Tags for this Thread