Thread: scanf is skipped in the run window

  1. #1
    Registered User
    Join Date
    May 2014
    Posts
    4

    scanf is skipped in the run window

    In the below written program,
    when i run it , it skips scanf and not allowing user to enter input.
    tried adding getchar(), fflush but nothing worked for me.
    can any one help me with this please.

    program code starts from here

    //file name : sort_array_of_structure.c
    Code:
    /*Write a program to accept records of the different states using array of structures. The structure should contain char state, population, literacy rate, and income. Display the state whose literacy rate is highest and whose income is highest.*/
    
    
    #include <stdio.h>
    #define M 50
    struct state
    {
        char name[50];
        long int population;
        float literacyRate;
        float income;
    }st[M]; /*array of structure*/
    int main()
     {
    
    
     int i, n, ml, mi, maximumLiteracyRate, maximumIncome;
     float rate;
     ml = mi = -1;
     maximumLiteracyRate = maximumIncome = 1;
    
    
     printf("Enter how many states:");
     scanf("%d", n);
     for (i = 1; i < n; i++)
        {
           printf("\nEnter state %d details : ",i);
    
    
           printf("\nEnter state name: ");
           scanf("%s",&st[i].name);
    
    
           printf("\nEnter total population : ");
           scanf("%d",&st[i].population);
    
    
           printf("\nEnter total literacy rate : ");
           scanf("%.2f",&rate);
           st[i].literacyRate = rate;
           
           printf("\nEnter total income : ");
           scanf("%f",&st[i].income);
          }
    
    
     for (i = 1; i < n; i++)
     {
          if(st[i].literacyRate >= maximumLiteracyRate)
              {
               maximumLiteracyRate = st[i].literacyRate;
               ml++;
              }
    
    
          if(st[i].income > maximumIncome)
              {
               maximumIncome = st[i].income;
               mi++;
              }
     }
     printf("\nState with highest literacy rate : %s",st[ml].name);
     printf("\nState with highest income : %s", st[mi].name);
    
    
     return(0);
     }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    increase your warning level

    line 24 - missing & in scanf
    line 31 - & in scanf should be removed
    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
    Join Date
    May 2014
    Posts
    4
    Hey vart,

    Thank you for your help.
    i made changes as you said and it worked
    thank you very much.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
     printf("Enter how many states:");
     scanf("%d", &n);
     for (i = 1; i < n; i++)
    Assume you enter 2 for the number of states... this loop will start with i equal to 1. Is 1 less than 2? Yes, continue with loop body. Next time through the variable i will be 2. Is 2 less than 2? No, therefore exit the loop.

    This loop will execute n-1 times because of your loop condition. If the user entered 1 for whatever reason then your program would skip the remaining user entry and determination of max literacy/income and go right to the output; which, since mi and ml are -1, would attempt to access memory for st[-1]. This would be bad.

    I would instead suggest:
    Code:
    for (i = 0; i < n; i++)
    ... along with checks to make sure what the user enters for n is <= M and > 1.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gets() skipped!
    By ncode in forum C Programming
    Replies: 14
    Last Post: 02-06-2012, 05:01 PM
  2. Scanf Being skipped
    By GibsMe in forum C Programming
    Replies: 2
    Last Post: 10-11-2009, 03:23 PM
  3. Scanf() skipped
    By new-b in forum C Programming
    Replies: 9
    Last Post: 07-18-2009, 01:34 AM
  4. scanf is being skipped
    By yougene in forum C Programming
    Replies: 6
    Last Post: 12-24-2008, 06:05 AM
  5. Second scanf keeps getting skipped.
    By yougene in forum C Programming
    Replies: 9
    Last Post: 12-23-2008, 02:39 AM

Tags for this Thread