请问我做错了什么,能否给我一些解释?这个程序是用来输入和显示学生信息的。它必须使用结构体并且有两个函数;一个是捕获(需要通过引用传递),另一个是显示(需要通过值传递)。
以下是我的代码:
#include <stdio.h>
struct Students{
int ID;
char name[50];
int age;
char address[100];
char course[30];
} aStudent[5];
void capture(char *name , int *age , char *address, char *course){
int i;
for(i=0; i<5; i++){
aStudent[i].ID = i+1;
printf("\nFor Student number %d:\n",aStudent[i].ID);
printf("Enter Student Name: ");
scanf ("%s", &aStudent[i].name);
printf("Enter Student Age: ");
scanf ("%d", &aStudent[i].age);
printf("Enter Student Address: ");
scanf ("%s", &aStudent[i].address);
printf("Enter Course: ");
scanf ("%s", &aStudent[i].course);
}
}
void display(char name, int age , char address, char course){
int i;
for(i=0; i<5; i++){
printf("\nStudent %d:\n",aStudent[i].ID);
printf("Name: %s\t\tAge: %d\t\tAddress: %s\t\tCourse:
%s",aStudent[i].name, aStudent[i].age, aStudent[i].address,
aStudent[i].course);
printf("\n");
}
}
void main()
{
int option, age;
char name, address, course;
printf("\t...Welcome to the Student Data System...\n\n");
printf("\nPlease Select An Option: \n1. Input Student Data\n2. View
Student Data\n3. Exit Syatem\n\n");
scanf("%d",&option);
switch(option){
case 1:
printf("Enter Student Details:\n");
capture(name, age , address, course);
break;
case 2:
printf("\nDisplaying Information:\n");
display(name, age , address, course);
break;
case 3:
close();
break;
default:
printf("\nSorry, your option is not valid.");
}
}
我已经测试了多次,它可以正常工作,但是我收到了以下错误消息:每个使用的参数都显示错误 此外,是否有一种方法或一行代码可以在完成一个case后返回switch的开头 - 即“返回主菜单”?
while(true) {...}
),在其中读取用户的输入。 - mnilledisplay
和capture
中传递参数却没有使用它们?另外,close
是什么?而且在 C 语言中没有 "按引用传递" 的概念。所有参数都是按值传递的。 - Jabberwocky