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/