0
    #include<stdio.h>
    #include<string.h>
    typedef struct
    { char name[20];
    long int ptn;
    float lt;
    }census;
    int main()
    { int i,n;
    census city[10];
    printf("How many cities\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    census city[10];
    printf("Name:");
    gets(city[i].name);
    printf("\nPopulation:");
    scanf("%ld",&city[i].ptn);
    printf("\nlteracy:");
    scanf("%f",&city[i].lt);
    }
    }
2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by Gonbe
0
  1. You declare "census city[10];" within the loop while it's also declared outside of it..
  2. The issue is similar to your other thread. See my awnser there: http://www.daniweb.com/software-development/c/threads/437105/scanf-is-skipping-in-last-input-.-if-i-use-int-instead-of-charcode-is-runn
  3. Use fgets() instead of gets().

Summary of the problem: With the first scanf a newline remains in the stream. gets() stops when reading a newline which is the first thing it reads now because it was left in the stream as mentioned.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.