Thread: ScanF Skipping The User Input.

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    8

    ScanF Skipping The User Input.

    Ok, so Im in first year of programming and Im having some difficulties, We're only using math.h,stdlib.h, stdio.h libraries..


    And we're doing a program which the user enters the clients.

    In:
    1
    Jhonny Bravo
    Front Ave. #21
    1984-05-13

    Out:

    Clients number:1
    Client Name: Jhonny Bravo
    Adress: Front Ave. #21
    DofBirth: 1984-05-13
    ...
    ..And so on..
    Heres the Code

    THe problem is that when Im using numbers or spaces the code wont wait for the user to input what is asked for next....

    Heres the code:




    Code:
    #include <stdio.h>#include <string.h>
    #include <stdlib.h>
     
    typedef struct clientes
    {
      int numerocliente;
      char nombre[30];
     char nacimiento[15];
      char direccion[30];
      struct Clientes *psig;
      struct Clientes *pant;
    }CLIENTE;
     
    int main(void)
    {
        struct clientes *pfirst =NULL;
        struct clientes *pcurrent =NULL;
        struct clientes *plast =NULL;
     
        char temp[15];
        int opcion=0;                    
         
        for(;;)
        {
            printf("\n\n   Elige:");
            printf("\n   1. Agregar Empleado");
            printf("\n   2. Eliminar");
            printf("\n   3. Mostrar Info de Empleado");
            printf("\n   4. Quit\n\n");
     
            printf("   La decision esta en tus manos: ");
            scanf("%d",&opcion);
     
            switch(opcion)
            {
                case 1:
                    if((pcurrent=(CLIENTE *)malloc(sizeof(CLIENTE)) )==NULL)
                    {
                        fprintf(stderr,"No hay Memoria\n");
                        exit(1);
                    }
     
                    if (pfirst==NULL)   
                    {
                        pfirst=pcurrent;
                        pcurrent->pant=NULL;
                    }       
                    else
                    {
                        plast->psig=pcurrent;
                        pcurrent->pant=plast;           
                    }
             
                    printf("\nDame el Numero de Cliente: \n");
                    scanf("%d", &pcurrent->numerocliente);
     
                    printf("Dame EL Nombre: \n");
    		scanf("%s",pcurrent->nombre);
     
                    printf("Dame la fecha de nacimiento(aaa-mm-dd): \n");
                    scanf("%s", temp);
                    strcpy(pcurrent->nacimiento,temp);
     
                    printf("Direccion del Cliente \n");
                    scanf("%s", pcurrent->direccion);
                
     
                    plast=pcurrent;     
                    break;
     
                case 2:
                    puts("Dame El Nombre a Eliminar: ");
                    scanf("%s", temp);
                         
     
                    break;
     
                case 3:
                    while(pcurrent != NULL)
                    {
                    printf("\n\nNombre del Cliente: %s\n Numero del Cliente: %d\n",pcurrent->nombre,pcurrent->numerocliente);
                    printf("\nFecha de Nacimiento del Cliente: %s\n, Direccion del Cliente: %s\n",pcurrent->nacimiento,pcurrent->direccion);
     
                    plast=pcurrent;
                    pcurrent=pcurrent->pant;
     
                    free(plast);        
                    }break;
             
                case 4: free(plast);
                        exit(1);
                        break;
                 
            }//end of switch    
        }//end of loop
     
        return 0;
    }
    Any feedback is accepted, and I thank you for your time!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    scanf expects you to account for every single character you enter, and it is line buffered, meaning you have to hit enter to actually have it do something with what you've been typing. That means you have to tell it to do something with the enter you have just typed.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    You should ever use return values from input-calls like scanf and you should ever clear the inputbuffer after every scanf.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by BillyTKid View Post
    clear the inputbuffer after every scanf.
    More accurately, you should only use scanf when you know the data will be exactly formatted the way you want - that's what the f in scanf means. If you are worried about garbage input, you should use something like fgets + sscanf.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    fgets not ensures a cleared inputbuffer, scanf ensures a not cleared inputbuffer and therefore its easier to handle it.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by BillyTKid View Post
    fgets not ensures a cleared inputbuffer
    Only if you don't have more input than what you've told it to read.
    Code:
    char buf[ 5 ];
    fgets( buf, 5, stdin );
    If you type 6 characters, you will still have input waiting.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Quote Originally Posted by quzah View Post
    Only if you don't have more input than what you've told it to read.
    Code:
    char buf[ 5 ];
    fgets( buf, 5, stdin );
    If you type 6 characters, you will still have input waiting.


    Quzah.
    and
    If you type 5 characters, you will still have input waiting, inputbuffer must cleared.
    If you type 4 characters, you will still have input waiting, inputbuffer must cleared.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Which is why I suggest using BUFSIZ as the size of the primary input buffer to use with fgets().

    Input, validation, conversion and storage are distinct steps, each of which could potentially go wrong.
    For example, input can fail at EOF, validation can fail by typing "hello" when an int is expected, conversion can fail with numeric overflow. Even storage can fail if you're building some dynamic data structure and you run out of memory.

    If the user is just being fat-fingered, and typing in a few extra characters by mistake, then the buffer will be cleared.

    If the abuser is being a script-kiddie wannabe trying buffer overflow, then perhaps the buffer would not be cleared, but so what? So long as the program doesn't crash, deliberate attempts at buffer overflow can just be eaten away with repeated calls to fgets().

    But if you really care about long lines (say parsing the output of the pre-processor), then this is easy to accomplish with fgets as well. All you need to do is detect that the last char of the buffer isn't \n, and then drop into "long line" mode, calling fgets until you get a buffer with \n in it.

    The naive while ( (ch=getchar()) != '\n' ); will lock up in an infinite loop if the user decides to redirect input from a file, and it hits EOF before hitting a newline.

    A checked return from fgets(), with a large buffer, followed by a checked return from sscanf() is easy enough to do, and gives obvious and repeatable outcomes for any user who isn't being a complete idiot (or malicious).
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program skipping input....Scanf()
    By steve5591 in forum C Programming
    Replies: 2
    Last Post: 11-17-2010, 06:53 PM
  2. Help, scanf isn't waiting for user input
    By Blasz in forum C Programming
    Replies: 5
    Last Post: 05-07-2010, 05:32 AM
  3. skipping input?
    By axon in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 01:32 PM
  4. skipping input
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 09-25-2002, 01:22 PM
  5. Skipping scanf()
    By Ican'tC in forum C Programming
    Replies: 4
    Last Post: 09-12-2002, 02:49 AM

Tags for this Thread