Thread: Problem With Structure

  1. #1
    Registered User
    Join Date
    Jan 2007

    Problem With Structure

    Hi ,

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

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

    struct student
      int roll_no;
      char name[25];
      char city[25];
      char course[10];
    int main()
    int number,i;
    struct student s[22];
    printf("\nEnter The No. Of Records to Enter");
    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.:");
     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 ????


  2. #2
    "I Win!" by U. Lose vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    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
    To be or not to be == true

  3. #3
    Join Date
    Oct 2007
    Inside my computer
    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, 09:53 AM
  2. Problem with structure and class
    By Bargi in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2007, 02:30 AM
  3. Problem with arrays inside a structure
    By babu in forum C Programming
    Replies: 4
    Last Post: 07-12-2007, 09:35 AM
  4. accessing structure pointer problem
    By godhand in forum C Programming
    Replies: 2
    Last Post: 04-09-2004, 10: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