如何修复“不调用结构化功能”

因此,我将这段代码编写为作业的一部分,就像这样,我们仍在学习C语言,但我无法对此进行更正。我们必须编写代码来编写函数,以打印出特定年份加入的所有学生的姓名。

它将运行代码直到switch 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",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",s[i].stRNo);
        printf("Enter Course of Student %d:\n",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个代码)

也是`

  1. 编写一个函数以打印特定年份加入的所有学生的姓名。
  2. 编写另一个功能以打印输入了卷号的学生的数据

制作开关盒。

我得到的输出:

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

预期输出:

在切换案例后输入Year之后,我希望调用函数`stYr并打印以该Year为加入年的学生的列表

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

hang116026 回答:如何修复“不调用结构化功能”

在学习C代码时,我认为您应该从一些标准的工作实践入手。 很明显,要么您的老师没有真正在工作环境中编码,要么他们希望您发现错误。 我意识到您的编译器可能存在一些问题或限制,但是这里有一些指针将在将来对您有所帮助。

以下是一些基于您的代码的指针。

  1. 始终在使用前初始化变量。如果变量可以为null且不是有效值,请在使用前检查null。 char []或指针类型。
  2. 使用typedef进行结构声明,这将允许编译器通过在编译过程中进行类型检查来确定不正确的用法。
  3. 使用有意义的长变量和函数名来帮助理解代码。
  4. 带有方括号的缩进,例如{ },使代码易于理解。
  5. 不要在代码中使用文字值,因为如果在所有情况下都未通过代码更改值,则可能会在以后引入错误。
  6. 函数参数的类型应明确-这使代码在编译器,环境之间可移植,并且更易于理解。
  7. 在for循环中,尝试仅使用少于<和大于>的比较。
  8. 在'if'语句中尝试将不可分配的值放在左侧,因为如果您键入=而不是==,则会引发编译器警告。
  9. 尝试使函数重新进入,即,如果可能的话,不要依赖全局变量来确定其行为,void func(void)会发出警钟。
  10. 在使用带符号的值(例如int)时要特别小心,除非您知道变量将被分配小于0的值,否则请优先使用unsigned int
  11. 将函数参数声明为const,然后使用局部函数变量作为副本(如果您希望更改其值)将使代码更易于调试。
  12. Switch语句应始终包含default:来处理不确定的值。

struct stdnt /* REVIEW COMMENT: use typedef */
{
    char stName[100]; /* REVIEW COMMENT: non-literal value for 100 */
    char stRNo[100]; /* REVIEW COMMENT: Based upon later usage should this be unsigned int rather than char[] type */
    char Dept[50];
    char crse[50];
    int YoJ[4]; /* REVIEW COMMENT: this is an array of int. Either use int or char[4] */
}s[2];          /* REVIEW COMMENT: separate out declaration for clarity and use a meaningful name */

我建议您将其更改为此

#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是一种定义很松的语言,由于编码中的捷径,很容易引入错误。 要学究。 不要让编译器做出决定。 确保变量的数据类型是所需的,而不是编译器确定的。

修改 考虑到我对数据类型的评论。 此行if(year == s[i].YoJ)执行以下if(int == int[4])

本文链接:https://www.f2er.com/3154627.html

大家都在问