gets() ignored

This is a discussion on gets() ignored within the C Programming forums, part of the General Programming Boards category; I have a problem with gets function. I've put gets as a statement for a switch case, which is inside ...

  1. #1
     is offline
    ex-samurai 's Avatar
    Join Date
    Nov 2006
    Location
    England
    Posts
    18

    gets() ignored

    I have a problem with gets function. I've put gets as a statement for a switch case, which is inside a loop. This gets seems to get ignored everythime i run my program. What have I done wrong?

    Code:
    do
    	{
    		printf("Your choice: ");
    		scanf("%d",&menu);
    
    		switch(menu)
    		{
    		case 1:
    			printf("Enter command name to look up: ");
    			gets(input);
    			printf("aaa\n");
    Basically, when i choose menu 1, the output is:

    Your choice: 1
    Enter command name to look up: aaa
    Your choice:

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    the first mistake that u have done in your programming life is using gets. Why still people use gets. Read FAQ. gets is dangerous. Look at this link.

    Gets is bad

    Dint you see your compiler warning message " *** gets() is dangerous ***"

    instead use fgets()

    ssharish2005
    Last edited by ssharish2005; 12-02-2006 at 08:15 AM.

  3. #3
     is offline
    ex-samurai 's Avatar
    Join Date
    Nov 2006
    Location
    England
    Posts
    18
    ummm, i tried fgets but it doesnt make any difference.
    Code:
    do
    	{
    		printf("Your choice: ");
    		scanf("%d",&menu);
    		
    		switch(menu)
    		{
    		case 1:
    			printf("Enter command name to look up: ");
    			fgets(input,15,stdin);
    The output is still:
    Your choice: 1
    Enter command name to look up: Your choice:

    Help please...
    Last edited by ; 12-02-2006 at 09:10 AM.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    well this is a very coomon questin on the board. But here u go the solution.

    Code:
    void clear_buffer(void)
    {
         int ch;
         while((ch=getchar()) != '\n' && ch != EOF);
    }
    the problem with you is your scanf(). When u read the value from the user and press enter. Scanf leaves you '\n' in your buffer. you got to clear that. To clear you input buffer use the above function. Call this function after your scanf. that would solve your problem.

    Search the form about this issue.

    ssharish2005

  5. #5
     is offline
    ex-samurai 's Avatar
    Join Date
    Nov 2006
    Location
    England
    Posts
    18
    it worked
    thx

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    Or drop using scanf() and use fgets() to real ALL input.

    Then use sscanf() or any other conversion which works on strings in memory to do the rest of the work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21