Thread: fgets in switch-case problem

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    4

    fgets in switch-case problem

    Hi,

    I have a problem with fgets inside a case where it doesn't even allow me to enter input, it just suddenly goes into the function after or back to the 'choose' menu, can someone tell me why it's like this and how to fix it? Doesn't seem to have any problems outside fo a switch-case statement.

    Code:
    #define MAX_INPUT 5
    char string[5];
    switch (choose())
    {
    	case 1:
    		printf("Enter no more than 5 characters:  \n");
    		fgets(string, MAX_INPUT, stdin);  
    		useInput(string);
    		break;
     	case 2:
    		funct2();
    		break;
    	case 3:
    		funct3();
    		break;
    
    }
    Last edited by icestorm; 08-20-2009 at 04:05 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Let me guess: choose() uses scanf to acquire input? Mixing scanf and fgets requires extreme care, and you should either choose one method or the other, or read up on how to mix them properly.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    4
    Yes you're right about that , guess I'll have to change it. =/

    Code:
    int choose(void)
    {
    	int option = 0;
    	printf("Menu: \n"
    	"1) Input 1 \n"
    	"2) Implementation \n"
    	"3) Directive \n"
    	"4) Quit \n"
    	"\n"
    	"Select your option: \n");
    	scanf("%d", &option);
    	printf("\n");
    
    	return option;
    
    
    }
    I also left out the fact that the switch case is inside a menu loop:
    Code:
    	while(quit != 1)
    	{
                  switch..
             ....
             ....
             ....
             }
    I'm guessing that is probably also a big part of the problem. Thanks anyway going to try and change it, and so far when I tried to change everything I kept on getting errors though when trying to make choose work with fgets.
    Last edited by icestorm; 08-20-2009 at 05:27 PM.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by icestorm View Post
    Yes you're right about that , guess I'll have to change it. =/

    Code:
    int choose(void)
    {
    	int option = 0;
    	printf("Menu: \n"
    	"1) Input 1 \n"
    	"2) Implementation \n"
    	"3) Directive \n"
    	"4) Quit \n"
    	"\n"
    	"Select your option: \n");
    	scanf("%d", &option);
    	printf("\n");
    
    	return option;
    
    
    }
    I also left out the fact that the switch case is inside a menu loop:
    Code:
    	while(quit != 1)
    	{
                  switch..
             ....
             ....
             ....
             }
    I'm guessing that is probably also a big part of the problem. Thanks anyway going to try and change it, and so far when I tried to change everything I kept on getting errors though when trying to make choose work with fgets.
    Hint: fgets() into a smallish buffer, say BUFSIZ (look it up) or even 32 bytes. Then use sscanf() on the buffer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-05-2009, 11:32 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM
  5. Extra printed stmts...why?
    By mangoz in forum C Programming
    Replies: 4
    Last Post: 12-19-2001, 07:56 AM