Thread: simple struct program but erros

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    75

    simple struct program but erros

    im making simple program to read roll number and name of 5 students but it only prints roll no of 1st student whats wrong in code?

    Code:
    #include<stdio.h>
    
    
    void main()
    {
    struct student
    {
        int roll;
        char name;
    };
    struct student stu[3];
    int i;
    printf("Enter details of 5 students\n");
    for(i=0;i<3;i++)
    {
        printf("Enter roll no for student %d\n",i+1);
        scanf("%d",&stu[i].roll);
        printf("Enter name of student %d\n",i+1);
        scanf("%s",&stu[i].name);
    }
    printf("details of student\n");
    for(i=0;i<3;i++)
    {
        printf("details of stuednt %d\n",i+1);
        printf("Roll no %d\n",stu[i].roll);
        printf("name %s",stu[i].name);
    }
    }
    i have to print roll no and name of 3 students not only of 1st studnt
    help me guys!!!

  2. #2
    Registered User
    Join Date
    Dec 2015
    Posts
    3
    Good morning san12345.

    First of all; You need to improve your "style" of coding. I mean leave blank lines when needed, so if you read it 2 or 3 months later, you won't try to figure out what the program does.

    So I tried to re-make your program using typedef. Struct is not needed to be inside main. Also, notice that the names are strings not characters.

    Code:
    #include<stdio.h>
    
    struct student
    {
        int roll;
        char name[50];
    };
    
    
    typedef struct student studentT;
    
    
    void main()
    {
        studentT stu[3];
        int i;
    
        printf("Enter details of 5 students\n");
    
        for(i=0;i<3;i++)
        {
            printf("Enter roll no for student %d\n",i+1);
            scanf("%d",&stu[i].roll);
    
    
            printf("Enter name of student %d\n",i+1);
            scanf("%s", &stu[i].name);
        }
        
        printf("details of student\n");
        
        for(i=0;i<3;i++)
        {
            printf("details of stuednt %d\n",i+1);
            printf("Roll no %d\n",stu[i].roll);
            printf("name %s",stu[i].name);
        }
    }
    If you have any questions, just ask

  3. #3
    Registered User
    Join Date
    Aug 2015
    Posts
    75
    thx for reply. your code is working but is it posible to make it using struct only... instead of typedef

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by san12345
    is it posible to make it using struct only... instead of typedef
    If you do not want the typedef then use struct student instead of studentT.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-01-2003, 10:58 AM
  2. erros
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 04-13-2002, 08:43 AM
  3. erros 2
    By prlove71 in forum C++ Programming
    Replies: 3
    Last Post: 03-13-2002, 05:36 PM
  4. erros
    By prlove71 in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2002, 05:04 PM