Thread: Another character input problem

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    9

    Another character input problem

    Hi. Here's the code:

    Code:
    /*	Jack White 090455775
    	EE Programming Fundamentals Lab 9
    	Question 3
    */
    
    #include <stdio.h>
    
    struct town_info {
        char name[59];   /*The longest place name in Britain is 58 characters long*/
        int d_from_lon;
    };
    
    int main(void) {
        struct town_info town[5];
        int counter;
        
        for (counter=0; counter < 5; counter ++) {
            printf("Enter a town: ");
            gets(town[counter].name);
            printf("Enter its distance from London (in miles): ");
            scanf(" %d", &town[counter].d_from_lon);
        }
        
        printf("\nThe following towns are less than 100 miles from London: \n");
    
        for (counter=0; counter < 5; counter ++) {
            if (town[counter].d_from_lon < 100) {
                puts(town[counter].name);
            }
        }
            
        return 0;
    }
    
    /*End of programme*/
    When it prints out the names at the end, the first character is missing from the first town. I already know about the problem with gets, btw, but I don't want to upset my lecturer. It doesn't work any better with fgets, or indeed scanf.

    Any ideas?

    Thanks.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Okay, when I run this, I don't even get that far:

    Enter a town: one
    Enter its distance from London (in miles): 5
    Enter a town: Enter its distance from London (in miles):


    Another "stray newline in the stdin buffer" problem, which is what I would expect.

    Read this:
    STDIN pitfalls
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Use fgets() instead of gets() because of potential buffer overflow. Also as noted earlier, try the following: (this will not protect you from input that is not expected!)

    Code:
       scanf("%d%*c", &town[counter].d_from_lon);
    Last edited by slingerland3g; 11-25-2009 at 02:53 PM. Reason: added note on buffer overflow

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    9
    Cheers! Those pesky buffers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking to shlwapi.lib in C, MSVC CMD.
    By Joerge in forum Windows Programming
    Replies: 4
    Last Post: 08-07-2009, 05:18 PM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Confusing problem with file input
    By Vorok in forum C++ Programming
    Replies: 3
    Last Post: 01-05-2003, 03:49 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM