我正在创建一个菜单驱动程序,使用两种结构(学生和班级)来存储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;
}
答案 0 :(得分:0)
我修复了您的代码。我可以检测到4个错误,我将其标记为。对于struct学生中的fullName。似乎您想要一个字符串,可以使用该选项,固定长度的数组或为此使用malloc和指向char(string)的指针。但这取决于您。第二个错误,在您的struct类别中,我认为Student应该是struct Student,而不是char数组。
Mistake 3-对于您的打印循环,您留下了下面标记为我的打印语句之一。那应该是一个i。我还为您的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;
}