c - 使用用户从结构输入的数据

标签 c function struct menu

我正在创建一个菜单驱动程序,使用两种结构(学生和班级)来存储20名学生的信息。

但是,我在单独的功能中显示用户输入的信息的功能不起作用。我是否假设有一种特定的方法可以将一个函数中的结构信息提供给另一个函数?

这是我的(未完成的)代码:

struct Student 
{
   char fullName;
   int ID;
   int finalMark;
};

struct Class
{
   char Student[20];
   char className;
   int classCode;
};

struct Student stud[20];

void optionTwo()
{
   int i;

   for (i=0; i<3; i++)
   {
       printf("Student %d \n", i+1);
       printf("Enter student name: \n");
       scanf("%s", &stud[i].fullName);
       printf("Enter student ID: \n");
       scanf("%d", &stud[i].ID);
   }
}

void optionThree()
{
    int i;

    for(i=0; i<3; i++)
    {
        printf("Student %d \n", i+1);
        printf("Student name %s \n", stud[1].fullName);
        printf("Student ID: %d \n", stud[i].ID);
    }
  }


int main()
{
    int choice;
    int end = 0;

    while(end != 1){

        printf("1 - Set class name and code \n");
        printf("2 - Add a new student to the class \n");
        printf("3 - Print class details \n");
        printf("4 - Find the average class mark \n");
        printf("5 - Print student details given ID \n");
        printf("6 - Exit \n");

        scanf("%d", &choice);
        printf("\n");

        switch(choice){
        case 1: {
            printf("\n Set class name and code: \n");
            break;
        }
        case 2: {
            printf("\n Add new student: \n");
            optionTwo();
            break;
        }
        case 3: {
            printf("\n Print class details \n");
            optionThree();
            break;
        }
       case 4: {
            printf("Option 4 \n");
            break;
        }
       case 5: {
            printf("Option 5 \n");
            break;
        }
       case 6: {
            printf("Option 6 \n");
            end = 1;
            break;
        }
       default:
            printf("Invalid Input");
        }


    }



    return 0;
}

最佳答案

我修复了您的代码。我可以检测到4个错误,我将它们标记了出来。对于struct学生中的fullName。似乎您想要一个字符串,可以使用该选项,固定长度的数组或为此使用malloc和指向char(string)的指针。但这取决于您。第二个错误,在您的struct类别中,我认为Student应该是struct Student而不是char数组。

错误3-对于您的打印循环,您留下了我标记在下面的打印语句之一。那应该是我。我还为您的scanf提供了缓冲流保护。

错误4-我没有为您解决这个问题,在您的struct类中,char className假定为char className [一个您认为最长的类名将要持续多长时间的值],就像在学生中使用fullName一样。切记为NULL字符添加一个额外的空间。当您有更多代码时,可以再次询问。

#include <stdio.h>
#include <stdlib.h>

struct Student 
{
   // 50 Character is probably enough for a full name
   // 50 + 1 character
   char fullName[51];
   int ID;
   int finalMark;
};

struct Class
{
   // This I think you meant struct student
   struct Student Student[20];
   char className;
   int classCode;
};

struct Student stud[20];

void optionTwo()
{
   int i;

   for (i=0; i<3; i++)
   {
       printf("Student %d \n", i+1);
       printf("Enter student name: \n");
       // Avoid buffer overflow
       scanf("%50s%*[^\n]", stud[i].fullName);
       printf("Enter student ID: \n");
       scanf("%d", &stud[i].ID);
   }
}

void optionThree()
{
    int i;

    for(i=0; i<3; i++)
    {
        printf("Student %d \n", i+1);
        // You left 1 here, it supposed to be i
        printf("Student name %s \n", stud[i].fullName);
        printf("Student ID: %d \n", stud[i].ID);
    }
  }


int main()
{
    int choice;
    int end = 0;

    while(end != 1){

        printf("1 - Set class name and code \n");
        printf("2 - Add a new student to the class \n");
        printf("3 - Print class details \n");
        printf("4 - Find the average class mark \n");
        printf("5 - Print student details given ID \n");
        printf("6 - Exit \n");

        scanf("%d", &choice);
        printf("\n");

        switch(choice){
        case 1: {
            printf("\n Set class name and code: \n");
            break;
        }
        case 2: {
            printf("\n Add new student: \n");
            optionTwo();
            break;
        }
        case 3: {
            printf("\n Print class details \n");
            optionThree();
            break;
        }
       case 4: {
            printf("Option 4 \n");
            break;
        }
       case 5: {
            printf("Option 5 \n");
            break;
        }
       case 6: {
            printf("Option 6 \n");
            end = 1;
            break;
        }
       default:
            printf("Invalid Input");
        }


    }



    return 0;
}

关于c - 使用用户从结构输入的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59220578/

相关文章:

c++ - 尝试在 C++ 中转换结构时出现奇怪的错误消息

r - 如果 "a"和 "b"在 R 中的前 4 个小数位相同,则认为它们相等

function - Haskell IO传递给另一个功能

c - 使用枚举时的高效 switch 语句

c - 在 C 套接字中保持客户端/服务器架构持续通信的最佳方法?

SQL Server 函数根据参数返回不同的选择语句

戈朗 : Type assign with another struct

c - 为什么客户端不能在这种情况下取​​消引用指针?

c - C语言如何重复一个字符串

C-Execl系统调用