如何在一个循环中打印所有id名称?

时间:2018-02-14 14:23:58

标签: c struct

此代码是关于' struct'在C ..

我创建了一个带有属性名称,roll等的结构。通过使用for-loop我让用户创建结构对象。他们被命名为学生,员工,教师

问题是对象是否已创建。但我只能在for循环中使用它们。如果我想获得main函数中所有名称的值,它就不起作用。我该如何解决?如何在一个循环中打印代码中的所有名称 什么是逻辑

#include<stdio.h>
#include<conio.h>  
struct student
{
    int std;
    char fee[90];                                 //Collect Data of students
    int rollno;
    char name[15];
    char sub[100];
};
main()
{
    int x;
    printf("******Enter the total number of Student from HOD*******:\n");
    scanf("%d",&x);
    struct student a[x];
    for(int i=0;i<x;i++)
    {
        printf("\nEnter Rollno:\t");
        scanf("%d",&a[x].rollno);
        printf("\nEnter name:\t");
        scanf("%s",&a[x].name);
        printf("\nIs Fee Submitted:\t");
        scanf("%s",&a[x].fee);
        printf("\nEnter Subject name:\t");
        scanf("%s",a[x].sub);
    }
    printf("\n****Display All Student names****");
    for(int i=0;i<x;i++)
    {
        printf("\n%s",a[x].name);
    }   


    //Faculty 
    struct faculty
    {
        char Fname[100];
        char Sname[100];
    };

    int y;
    printf("\n\n********Please HOD enter the total faculty members********\n");
    scanf("%d",&y);
    struct faculty b[y];
    for(int j=0;j<y;j++)
    {
        printf("\nEnter Faculty Member Name:\t");
        scanf("%s",&b[y].Fname);
        printf("\nEnter their Subjects:\t");
        scanf("%s",&b[y].Sname);
    }
    printf("\n****Display all Faculty Member Name****");
    for(int j=0;j<y;j++)
    {
        printf("\n%s",b[y].Fname);
    }

    // Employes
    struct employes
    {
        char ename[100];
        char rank[100];
    };
    int z;
    printf("\n\n********please HOD enter the total no of Employes*******:\n");
    scanf("%s",&z);
    struct employes c[z];
    for(int j=0;j<y;j++)
    {
        printf("\nEnter the Employe name:\t");
        scanf("%s",&c[y].ename);
        printf("\n and enter their ranks:\t");
        scanf("%s",&c[y].rank);

    }
    printf("\n****Display all Employe names****");
    for(int j=0;j<y;j++)
    {
        printf("%s\n",c[y].ename);
    }

}

2 个答案:

答案 0 :(得分:0)

循环将i0增加到其他值x (假设此示例中x为5)

所以i的值为0,1,2,3 ....

但无论如何,你只将数据放入数组的一个元素中:5
并且你一遍又一遍地覆盖同一个元素 5甚至不是一个有效的指数!!有效指标为0,1,2,3,4 5是阵列末尾的一个!

答案 1 :(得分:0)

#include<stdio.h>
#include<conio.h>  
struct student
{
    int std;
    char fee[90];                                 
    int rollno;
    char name[15];
    char sub[100];
};
main()
{
    int x;
    printf("******Enter the total number of Student from HOD*******:\n");
    scanf("%d",&x);
    struct student a[x];
    for(int i=0;i<x;i++)
    {
        printf("\nEnter Rollno:\t");
        scanf("%d",&a[x].rollno);
        printf("\nEnter name:\t");
        scanf("%s",&a[x].name);
        printf("\nIs Fee Submitted:\t");
        scanf("%s",&a[x].fee);
        printf("\nEnter Subject name:\t");
        scanf("%s",a[x].sub);
    }
    printf("\n****Display All Student names****");
    for(int i=0;i<x;i++)
    {
        printf("\n%s",a[x].name);
    }   

    struct faculty
    {
        char Fname[100];
        char Sname[100];
    };

    int y;
    printf("\n\n********Please HOD enter the total faculty members********:\n");
    scanf("%d",&y);
    struct faculty b[y];
    for(int j=0;j<y;j++)
    {
        printf("\nEnter Faculty Member Name:\t");
        scanf("%s",&b[y].Fname);
        printf("\nEnter their Subjects:\t");
        scanf("%s",&b[y].Sname);
    }
    printf("\n****Display all Faculty Member Name****");
    for(int j=0;j<y;j++)
    {
        printf("\n%s",b[y].Fname);
    }

    struct employes
    {
        char ename[100];
        char rank[100];
    };
    int z;
    printf("\n\n********Please HOD enter the total no of Employes*******:\n");
    scanf("%d",&z);            //You used %s instead of %d
    struct employes c[z];
    for(int j=0;j<z;j++)  //You used wrong variable 'y' here
    {
        printf("\nEnter the Employe name:\t");
        scanf("%s",&c[z].ename);  //You used wrong variable 'y' here
        printf("\nEnter their ranks:\t");  
        scanf("%s",&c[z].rank);  //You used wrong variable 'y' here

    }
    printf("\n****Display all Employe names****");
    for(int j=0;j<z;j++)  //You used wrong variable 'y' here
    {
        printf("\n%s",c[z].ename);  //You used wrong variable 'y' here
    }
}

错误:

  1. 错误的格式说明符用于在Employees数量中获取整数输入。
  2. 在Employee的所有循环中使用错误的循环变量。
  3. 现在这段代码正常运行。我也在评论中指出了错误。