【发布时间】:2016-03-31 01:22:22
【问题描述】:
我已经在这个烂摊子上混了一段时间,但我仍然没有弄清楚我哪里出了问题,如果它像指针这样荒谬的话,我会完全自欺欺人。
显示的任务:尝试用学生 ID、姓名、姓氏、出生日期和成绩填充结构数组。然后通过提供给用户的匹配 ID 进行搜索。
我非常感谢与此主题相关的任何帮助,我已经严重坚持了一段时间。另外我提前为法语部分道歉
// Part 1
struct Date{
int day;
int month;
int year;
};
// Part 2
struct Student{
int ID;
char name[20];
char lastname[20];
struct Date DOB;
int notes[J];
};
// Part 3
void FillStudentList(struct Student E){
int i;
printf("\nInsert ID: ");
scanf("%d", &E.ID);
printf("Insert name: ");
scanf("%s", &E.name);
printf("Insert last name: ");
scanf("%s", &E.lastname);
printf("Insert date of birth: ");
scanf("%d %d %d", &E.DOB.day, &E.DOB.month, &E.DOB.year);
printf("Insert notes: ");
for(i=0; i<J; i++)
scanf("%d", &E.Notes[i]);
}
// Part 4
void ShowByNb(int Nb, struct Student E[], int NbStudents){
int j, i;
for(i=0; i<NbStudents; i++){
if (E[i].ID== Nb){
printf("\nID: %d", E[i].ID);
printf("\nName: %s", E[i].name);
printf("\nLast Name: %s", E[i].lastname);
printf("\nDate Of Birth: %s-%s-%s", E[i].DOB.day, E[i].DOB.month, E[i].DOB.year);
printf("\nNotes: ");
for(j=0; j<J; j++){
printf("%d", E[i].Notes[j]);
}
}
else
printf("\nInvalid Student!\n");
}
}
// Part 5
void main(){
int i, x;
struct Student E[N];
for(i=0; i<N; i++){
printf("\n\nStudent #%d", i+1);
FillStudentList(E[i]);
}
printf("\n\nSearch student by NB: ");
scanf("%d", &x);
ShowByNb(x, E, N);
}
【问题讨论】:
-
你得到的输出是什么?请为您的变量和函数使用更好的名称。
-
Dat的元素是int,但您使用%s而不是%d来阅读它们。那是行不通的。 -
@ViniciusZaramella 抱歉,这是一个法国项目,所以这就是为什么名称是法语的原因。我得到的输出是“无效的学生!”当我尝试进行应该有效的搜索时。它被写了两次。编辑:我修好了
-
@Barmar 对不起,我复制了旧版本的代码,我确实将 %s 修改为 %d,但它仍然无法正常工作。
-
@Barmar,我指的是长度为 1 或 2 位的变量......如 E、x、Nb。这只是一种不好的做法,会使您的代码难以阅读。