Thread: Inputting Data Fields - Rogue Enter?

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    8

    Inputting Data Fields - Rogue Enter?

    Hi, I need to enter a few data fields in a row, some of which are strings which will contain spaces. I have the code below which works fine apart from when I press enter for the membership number it skips the name field.
    Also, pressing enter for the address lines skips a line in the console printing the next request for input two lines down. This is minor as it's purely a cosmetic problem but does anyone have any ideas?

    Code:
        printf("\nMembership Number: ");
        scanf("%d", &temprec.membershipno);
        printf("\nName:");
        fgets(temprec.name, 25, stdin);
        temprec.name[strlen(temprec.name)-1] = 0;
        printf("\nAddress Line One:");
        fgets(temprec.addressone, 25, stdin);
        temprec.addressone[strlen(temprec.addressone)-1] = 0;
        printf("\nAddress Line Two:");
        fgets(temprec.addresstwo, 25, stdin);
        temprec.addresstwo[strlen(temprec.addresstwo)-1] = 0;
        printf("\nAge: ");
        scanf("%d", &temprec.age);
    Many thanks in advance!

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    don't mix fgets and scanf
    read everything with fgets
    then if needed - use sscanf to parse the string.

    scanf leaves the \n in the input buffer
    it is read by the next fgets.

    PS. Also there is a better way to remove \n from the read string:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Dont use scanf function to read a string. Use fgets for to read string, always. Should read FAQ for more information.

    Or if u are using scanf for reading a string, use this function to clear the buffer

    Code:
    void clear_buffer(void)
    {
        int ch;
    
        while((ch=getchar()) != '\n' && ch != EOF);
    }
    .

    And make note. Seach the form about this issue, there might a 1000 of post with the same issue with different solution.

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printf statement executed twice
    By ananddr in forum C Programming
    Replies: 4
    Last Post: 05-07-2009, 12:17 AM
  2. need help why inputting enter works?
    By darshanpatelh in forum C Programming
    Replies: 3
    Last Post: 09-21-2008, 10:46 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. terminate 0 - PLEASE HELP
    By Unregistered in forum C Programming
    Replies: 11
    Last Post: 11-21-2001, 07:30 AM
  5. Replies: 0
    Last Post: 10-22-2001, 12:02 AM