gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:09:02 25 4
gpt4 key购买 nike

我正在创建一个菜单驱动程序,使用两种结构(学生和班级)来存储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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com