如何修复“structuredfunctions未调用”

如何修复“structuredfunctions未调用”,c,C,所以我写了这段代码作为我作业的一部分,就像这样,我们仍在学习C语言,我只是无法纠正这一点。我们必须在编写函数时编写一个代码,以打印在特定年份加入的所有学生的姓名 它运行代码直到切换到case,但不再继续工作 #include<stdio.h> struct stdnt { char stName[100]; char stRNo[100]; char Dept[50]; char crse[50]; int YoJ[4]; }s[2]; vo

所以我写了这段代码作为我作业的一部分,就像这样,我们仍在学习C语言,我只是无法纠正这一点。我们必须在编写函数时编写一个代码,以打印在特定年份加入的所有学生的姓名

它运行代码直到切换到case,但不再继续工作

#include<stdio.h>
struct stdnt
{
    char stName[100];
    char stRNo[100];
    char Dept[50];
    char crse[50];
    int YoJ[4];
}s[2];

void stYr(p)
{
   printf("Student Name = %s\t",s[p].stName);
}

void stRNm(q)// q RNm
{
  int i;

    for(i=0;i<=1;i++)
    {if(q == s[i].stRNo)
        {
        printf("Student Name = %s\t",s[i].stName);
        printf("Student Year of Joining = %d\t",s[i].YoJ);
        printf("Student Department = %s\t",s[i].Dept);
        printf("Student Course = %s\n",s[i].crse);
        }
    }
}
int main()
{
    int i;
    int year;
    int RNm;
    int ch;
    printf("Please Enter the details of 2 Students.\n");
    for(i=0;i<=1;i++)
    {
        printf("Enter Name of Student %d:\n",i+1);
        scanf("%s",s[i].stName);
        printf("Enter Department of Student %d:\n",i+1);
        scanf("%s",s[i].Dept);
        printf("Enter Year of Joining of Student %d:\n",i+1);
        scanf("%d",s[i].YoJ);
        printf("Enter Roll Number of Student %d:\n",i+1);
        scanf("%s",s[i].stRNo);
        printf("Enter Course of Student %d:\n",i+1);
        scanf("%s",s[i].crse);
    }


    printf("Enter 1 to Print the List of Student using Year.\nEnter 2 to Search the details of student using Roll no.\n");
    scanf("%d",&ch);
    switch(ch)
    {
    case 1:

        printf("Enter the Year:\n");
        scanf("%d",&year);
        for(i=0;i<=1;i++)
        {
            if(year == s[i].YoJ)
            {
                stYr(i);
            }
        }
      break;
    case 2:

        printf("Enter the roll number you want to search:\n");
        scanf("%d",&RNm);
        stRNm(RNm);
        break;

    }
    return 0;

}
问题是

创建一个结构来指定以下给出的学生数据:卷号、姓名、部门、课程、加入年份

假设有不超过450名学生,我试图首先为2编写代码,一旦它成功,我将为450名学生编写代码

也`

写一个函数来打印在特定年份加入的所有学生的名字。 编写另一个函数来打印输入卷号的学生的数据 制作开关箱

我得到的输出:

请输入2名学生的详细信息。 输入学生1的姓名: 卡蒂亚 输入部门: 机械 输入加入年份: 2019 输入卷号: 1. 进入课程: C 输入学生2的姓名: 呼吸 输入部门: 机械 输入加入年份: 2019 输入卷号: 2. 进入课程: C 输入1以打印使用年份的学生列表 输入2以使用卷号打印学生的详细信息 1. 输入年份: 2019 进程返回0执行时间30.600秒 按任意键继续

预期产出:

在switch case之后输入年份后,我希望调用函数'stYr并打印以该年份作为加入年份的学生列表


同样,如果输入了卷号,我希望打印该学生的详细信息。

当您学习C代码时,我认为您应该先学习一些标准的工作实践。 很明显,要么你的老师没有真正在工作环境中编写代码,要么他们希望你发现错误。 我意识到,由于编译器的原因,可能会有一些问题或限制,但这里有一些将在将来有所帮助的提示

下面是一些基于代码的指针

始终在使用前初始化变量。如果一个变量可以为null,但这不是有效值,请在使用前检查null,例如`char[]或指针类型。 对结构声明使用typedef,这将允许编译器在编译期间通过类型检查来确定不正确的使用。 使用长而有意义的变量和函数名来帮助理解代码。 用括号缩进,例如{},使代码易于理解。 不要在代码中使用文字值,因为如果在所有情况下都没有通过代码更改某个值,这可能会在以后的阶段引入错误。 函数参数的类型应该是确定的——这使得代码在编译器、环境之间可以移植,并且更容易理解。 在for循环中,尝试仅使用少于的比较。 在“if”语句中,尝试将不可赋值的值放在左侧,因为如果键入=而不是==,则会引发编译器警告。 尝试使函数可重入,即不依赖全局变量来确定其行为。如果可能,void funcvoid应该敲响警钟。 使用有符号值(如int)时要小心,除非您知道变量的赋值小于0,否则应优先使用无符号int。 如果希望更改函数参数的值,则将函数参数声明为常量,然后使用局部函数变量作为副本,这将使代码更易于调试。 Switch语句应始终包含一个默认值:用于处理未确定的值。 我建议你改成这个

#define TEXT_LENGTH_LONG 100
#define TEXT_LENGTH_SHORT 50
#define NUMBER_OF_STUDENTS 2

typedef struct
{
    char Name[NAME_LENGTH];
    unsigned int RollNumber;
    char Department[TEXT_LENGTH_SHORT];
    char Course[TEXT_LENGTH_SHORT];
    unsigned int YearOfJoining;
}student_t;

student_t students[NUMBER_OF_STUDENTS];
此函数代码有一些问题

void stRNm(q)/* REVIEW COMMENT: non-definitive declaration can cause confusion. Unclear function name and parameter */
{
  int i;

    for(i=0;i<=1;i++)       /* REVIEW COMMENT: use of literal value  makes maintenance more difficult and can introduce bugs if the array size is changed but this is missed */
    {if(q == s[i].stRNo)    /* REVIEW COMMENT: possible type mismatch in comparison - comparing int or char[] ?*/
        {
        printf("Student Name = %s\t",s[i].stName); 
        printf("Student Year of Joining = %d\t",s[i].YoJ);
        printf("Student Department = %s\t",s[i].Dept);
        printf("Student Course = %s\n",s[i].crse);
        }
    }
}
变成

void PrintStudentInfo(const unsigned int rollNumOfStudent)/* Declaring rollNumOfStudent as const will give it compiler protection against being assigned to */
{
    int i;

    for(i=0;i<NUMBER_OF_STUDENTS;i++)
    {
        if(rollNumOfStudent == students[i].RollNumber) 
        {
            printf("Student Name = %s\t",students[i].Name);
            printf("Student Year of Joining = %d\t",students[i].YearOfJoining);
            printf("Student Department = %s\t",students[i].Department);
            printf("Student Course = %s\n",students[i].Course);
        }
    }
}
好的,这只是一个开始。 在你的主要职能中,有一些问题与我已经提到的有关,导致了这种奇怪的行为

C是一种定义非常松散的语言,由于编码中采用了快捷方式,很容易引入错误。 要学究气。 不要让编译器来做决定。 确保变量的数据类型是您想要的,而不是编译器确定的

编辑 考虑到我对数据类型的评论。
这行ifeer==s[i]。YoJ正在执行以下ifint==int[4]

答案中的格式有自己的特点。最好只使用`或缩进4个空格,因为您可能会像刚才那样漏掉一个空格。我通常在枚举后插入空[],然后插入代码,因为在枚举中,代码块应该缩进8个空格,这在一开始并不直观。我可以试着编辑这篇文章。Och,我不同意第2点。Re 2:typedef应该允许哪些额外的类型检查,如果没有这些检查是不可能的?这些是一般编程提示。它们与问题有什么关系?哪一个解决了问题?@KamilCuk-感谢您的输入,出于某种原因,代码块中的复制和粘贴没有起作用,预览中只显示了代码的第一行。如果你能正确编辑它,那就做我的客人吧
t、 我同意作者不应该在解决方案中得到一点帮助,而应该学到一些东西。我缺少的是回答问题的部分。或者至少它不应该隐藏在代码中的注释中;这段对话已经结束。
void PrintStudentInfo(const unsigned int rollNumOfStudent)/* Declaring rollNumOfStudent as const will give it compiler protection against being assigned to */
{
    int i;

    for(i=0;i<NUMBER_OF_STUDENTS;i++)
    {
        if(rollNumOfStudent == students[i].RollNumber) 
        {
            printf("Student Name = %s\t",students[i].Name);
            printf("Student Year of Joining = %d\t",students[i].YearOfJoining);
            printf("Student Department = %s\t",students[i].Department);
            printf("Student Course = %s\n",students[i].Course);
        }
    }
}