Thread: gets(), scanf(), fgets() problem!

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    1

    gets(), scanf(), fgets() problem!

    Hi ! I'm using gets(), fgets() or scanf() in my code but they doesn't seem to work properly. When running the code sometimes it passes to the next line without waiting for the user to enter nothing from keyboard.
    The problem is always with strings not with integers or characters

    Code:
    int main ()
    {
    	struct articulo unArticulo;
    	char usuario[10];
    	char marito[]="marito";
    	char JM[]="JM";
    	char promt[]="Control de Stock: Raffo & Burgues Inc.";
    	char SKU[10];
    	char sino;
    	int menu=-1;
    	
    	printf("%s\n",promt);
    
    	do
    	{
    // THIS gets(usuario) works fine!
    		printf("Ingrese nombre de usuario:\n");
    		fflush(stdin);
    		gets(usuario);
    		if(strcasecmp(usuario,marito)!=0 && strcasecmp(usuario,JM)!=0) printf("Nombre de usuario incorrecto.\n");
    	}
    	while(strcasecmp(usuario,marito)!=0 && strcasecmp(usuario,JM)!=0);
    	system("clear");
    	printf("%s\nBienvenido %s!! :-)\n",promt,usuario);
    	while(menu==-1)
    	{
    		printf("Ingrese la opcion deseada:\n0- Ver stock actual\n1- Declarar venta\n2- Declarar compra\n3- Buscar articulo por SKU\n\nIngrese la opción deseada: ");
    		fflush(stdin);
    		scanf("%d",&menu);
    		if (menu==0) ver_stock();
    		else if(menu==1) declarar_venta(usuario);
    		else if(menu==2) declarar_compra(usuario);
    		else if(menu==3)
    		{
    // THIS gets(SKU) won't wait to the user to enter the value and jumps to the next line.
    //(as shown on pic)
    			printf("Ingrese SKU: ");
    			fflush(stdin);
    			gets(SKU);
    			if(buscar_SKU(SKU)==-1)
    			{
    				 printf("SKU no encontrado, desea dar de alta? (s/n): ");
    				 fflush(stdin);
    				 scanf("%c",&sino);
    				 if(sino=='s' || sino=='S') nuevo_articulo(SKU);				 
    			}
    			else printf("Articulo %s\n%s\nPrecio de lista: U$S %f\n", unArticulo.SKU,unArticulo.nombre,unArticulo.precio_lista);
    		}
    		printf("\nPresione una tecla para continuar...\n");
    		fflush(stdin);
    		getchar();
    		menu=-1;
    	}
    	return 0;
    }
    Here is a screenshot of the running program.

    http://img703.imageshack.us/i/pantallazob.jpg/
    Last edited by securetux; 03-26-2011 at 09:50 AM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Never use gets!
    You probably have new lines left in the input buffer. Read this and see if it helps.

    EDIT: fflush(stdin) is wrong.
    Last edited by anduril462; 03-26-2011 at 10:05 AM.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also fflush() is undefined for input streams. It may compile with your compiler but it will not produce the desired results. See this link.

    Jim

  4. #4
    C Newbie
    Join Date
    Aug 2010
    Posts
    29

    Dont use gets()

    You should replace gets with scanf() or fgets().

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    71
    You should only use fgets() beacause accept spaces
    gets() - never use
    scanf() use only for numbers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf problem
    By tuurb046 in forum C Programming
    Replies: 15
    Last Post: 10-30-2007, 03:40 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Problem with RPC and scanf
    By Ricardo_R5 in forum C Programming
    Replies: 11
    Last Post: 01-08-2007, 06:15 PM
  4. scanf problem
    By Alexthunder in forum C Programming
    Replies: 6
    Last Post: 10-04-2006, 09:55 PM
  5. simple shell (c), fgets problem
    By razza in forum Linux Programming
    Replies: 6
    Last Post: 05-26-2002, 10:44 PM