Problem With Structure

This is a discussion on Problem With Structure within the C Programming forums, part of the General Programming Boards category; Hi , I am surprised with the output of structure program........ I have created a simple program of structure for ...

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

    Problem With Structure

    Hi ,

    I am surprised with the output of structure program........

    I have created a simple program of structure for student record.

    Code:
    #include<stdio.h>
    #include<string.h>
    
    struct student
    {
      int roll_no;
      char name[25];
      char city[25];
      char course[10];
    };
    
    int main()
    {
     
    int number,i;
    
    struct student s[22];
    printf("\n STUDENT RECORD PROGRAME");
    
    printf("\nEnter The No. Of Records to Enter");
    scanf("%d",&number);
    
    printf("\nEnter The Data Of students");
    
    for(i= 1;i<=number; i++)
    {
    
     
     printf("\nEnter Name:");
     fgets(s[i].name, sizeof(s[i].name), stdin);
    printf("\nEnter Roll No.:");
     scanf("%d",&s[i].roll_no);
     printf("\nEnter City:");
    fgets(s[i].city, sizeof(s[i].city), stdin);
     printf("\nEnter Course:");
    fgets(s[i].course, sizeof(s[i].course), stdin);
    }
    
    printf( "\n The Details Of Student Are:\n");
    
    for(i= 1;i<=number; i++)
    {
    
     printf("\n Enter Name: %s",s[i].name);
     printf("\nRoll No.:%d",s[i].roll_no);
     printf("\n  City:%s",s[i].city);
     printf("\n Course:%s",s[i].course);
    
    }
    
    }

    But When I run the program it doesn't ask for name i.e it's directly jump to roll No.....and when I enter roll no. then jump to course not asking for city..........

    Please help me where I am wrong ????

    Thanks

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    scanf leaves the '\n' in the input stream
    fgets then reads this as the whole string

    One of the possible solutions - use only fgets to read the user input

    and when needed parse the buffer with sscanf
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem getting structure to work in my function
    By Tom Bombadil in forum C Programming
    Replies: 18
    Last Post: 05-28-2009, 10:53 AM
  2. Problem with structure and class
    By Bargi in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2007, 03:30 AM
  3. Problem with arrays inside a structure
    By babu in forum C Programming
    Replies: 4
    Last Post: 07-12-2007, 10:35 AM
  4. accessing structure pointer problem
    By godhand in forum C Programming
    Replies: 2
    Last Post: 04-09-2004, 11:52 PM
  5. Problem checking for numeric value in a structure
    By ronkane in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2002, 02:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21