gpt4 book ai didi

c - 姓名和姓氏搜索功能将无法使用。为什么?

转载 作者:行者123 更新时间:2023-11-30 20:39:04 26 4
gpt4 key购买 nike

我创建了一个函数,因为我的项目和名称搜索/姓氏搜索函数无法正常工作并给出运行时错误。我实在看不出其中的错误。那么到底出了什么问题呢?

此外,ID和Mark搜索系统也存在一些错误,每当您搜索ID时,它也会带来mark = 0,同样,如果您搜索Mark,它也会带来ID = 0。

完整代码:

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

struct Student
{
long long int id;
char firstname[20];
char lastname[20];
int mark;
} student;

void
storeRecord()
{
FILE *fp;

printf("\nEnter Student Details:\n\nID number: ");
scanf_s("%lld",&student.id);

printf("\nName:");
scanf_s("%19s",student.firstname);

printf("\nSurname:");
scanf_s("%19s",student.lastname);

printf("\nMark(0 - 100 integer) : ");
scanf_s("%d",&student.mark);

fp = fopen("studentfile.txt","a+"); /* check if the file was opened */
if (fp == NULL)
return;
fprintf(fp, "\n%lld\t%s\t%s\t%d\t",
student.id,
student.firstname,
student.lastname,
student.mark);
fclose(fp);

printf("A student record has been added successfully...\n");
getchar();
}

int
compareStudentsById(struct Student lhs, struct Student rhs)
{
return (lhs.id == rhs.id);
}

int
compareStudentsByName(struct Student lhs, struct Student rhs)
{
return (strcmp(lhs.firstname, rhs.firstname) == 0);
}

int
compareStudentsByLastame(struct Student lhs, struct Student rhs)
{
return (strcmp(lhs.lastname, rhs.lastname) == 0);
}

int
compareStudentsByMark(struct Student lhs, struct Student rhs)
{
return (lhs.mark == rhs.mark);
}

void
printStudent()
{
printf("\nThe record is found.\n");
printf("\nID: %lld\nName: %s\nSurname: %s\nMark: %d \n",
student.id,
student.firstname,
student.lastname,
student.mark
);
}

void
searchStudent(int(*compare)(struct Student,struct Student), const char *const name, const char *const lastname, long long int id, int mark)
{
FILE *fp;
int found;
int matches;


if (name != NULL)
printf("Searching record with Name = %s.\n", name);
if (lastname != NULL)
printf("Searching record with Surname = %s.\n", lastname);
if (id != -1)
printf("Searching record with ID = %lld.\n", id);
if (mark != -1)
printf("Searching record with Mark = %d.\n", mark);

found = 0;
fp = fopen("studentfile.txt", "r");
if (fp == NULL)
{
printf("IO error\n");
return;
}

do
{
struct Student other;

if (name != NULL)
strcpy(other.firstname, name);
if (lastname != NULL)
strcpy(other.lastname, lastname);

other.id = id;
other.mark = mark;
matches = fscanf(fp,"\n%lld\t%s\t%s\t%d\t",
&student.id,
student.firstname,
student.lastname,
&student.mark);

if (matches == 4)
found = (compare(student, other) != 0);

} while ((matches == 4) && (found == 0));

if (found != 0)
printStudent();
else
printf("Not found...\n");

getchar();
}

void
searchStudentByName()
{
char studentname[20];

printf("\nEnter student first name: ");
scanf_s("%19s", studentname);

searchStudent(compareStudentsByName, studentname, NULL, NULL, NULL);
}

void
searchStudentById()
{
long long int id;

printf("\nEnter ID: ");
scanf_s("%lld", &id);

searchStudent(compareStudentsById, NULL, NULL, id, NULL);
}

void
searchStudentByLastname()
{
char studentlastname[20];

printf("\nEnter student surname: ");
scanf_s("%19s", &studentlastname);

searchStudent(compareStudentsByLastame, NULL, studentlastname, NULL, -1);
}

searchStudentByMark()
{
int mark;

printf("\nEnter Mark: ");
scanf_s("%d", &mark);

searchStudent(compareStudentsByMark, NULL, NULL, NULL, mark);
}

int main()
{

int choice;

choice = 0;
while (choice != 5)
{
printf("\n\tC PROGRAM OF STUDENT DATABASE SYSTEM");
printf("\n1 -> Store a new record in database\n");
printf("2 -> Search a student record by Student First Name\n");
printf("3 -> Search a student record by ID\n");
printf("4 -> Search a student record by Surname\n");
printf("5 -> Search a student record by Mark\n");
printf("6 -> Quit Student Database");
printf("\n\n");
printf("Enter your choice : ");

scanf_s("%d",&choice);
switch(choice)
{
case 1:
storeRecord();
break;
case 2:
searchStudentByName();
break;
case 3:
searchStudentById();
break;
case 4:
searchStudentByLastname();
break;
case 5:
searchStudentByMark();
break;
}
}
getch();
return 0;
}

最佳答案

如果您在打开警告的情况下编译了这段代码(每个人都应该这样做)并修复了警告,那么这个(修改后的)程序基本上可以工作。马克搜索可以工作,但会结束程序。我会把它留给你来解决。

void
printStudent()
{
printf("\nThe record is found.\n");
printf("\nID: %lld\nName: %s\nSurname: %s\nMark: %d \n",
student.id,
student.firstname,
student.lastname,
student.mark
);
}

void
searchStudent(int(*compare)(struct Student,struct Student), const char *const name, const char *const lastname, long long int id, int mark)
{
FILE *fp;
int found;
int matches;


if (name != 0)
printf("Searching record with Name = %s.\n", name);
if (lastname != 0)
printf("Searching record with Surname = %s.\n", lastname);
if (id != -1)
printf("Searching record with ID = %lld.\n", id);
if (mark != -1)
printf("Searching record with Mark = %d.\n", mark);

found = 0;
fp = fopen("studentfile.txt", "r");
if (fp == 0)
{
printf("IO error\n");
return;
}

do
{
struct Student other;

if (name != NULL)
strcpy(other.firstname, name);
if (lastname != NULL)
strcpy(other.lastname, lastname);

other.id = id;
other.mark = mark;
matches = fscanf(fp,"\n%lld\t%s\t%s\t%d\t",
&student.id,
student.firstname,
student.lastname,
&student.mark);

if (matches == 4)
found = (compare(student, other) != 0);

} while ((matches == 4) && (found == 0));

if (found != 0)
printStudent();
else
printf("Not found...\n");

getchar();
}

void
searchStudentByName()
{
char studentname[20];

printf("\nEnter student first name: ");
scanf("%19s", studentname);

searchStudent(compareStudentsByName, studentname, 0, 0, 0);
}

void
searchStudentById()
{
long long int id;

printf("\nEnter ID: ");
scanf("%lld", &id);

searchStudent(compareStudentsById, 0, 0, id, 0);
}

void
searchStudentByLastname()
{
char studentlastname[20];

printf("\nEnter student surname: ");
scanf("%19s", studentlastname);

searchStudent(compareStudentsByLastame, 0, studentlastname, 0, -1);
}

searchStudentByMark()
{
int mark;

printf("\nEnter Mark: ");
scanf("%d", &mark);

searchStudent(compareStudentsByMark, 0, 0, 0, mark);
}

int main()
{

int choice;

choice = 0;
while (choice != 5)
{
printf("\n\tC PROGRAM OF STUDENT DATABASE SYSTEM");
printf("\n1 -> Store a new record in database\n");
printf("2 -> Search a student record by Student First Name\n");
printf("3 -> Search a student record by ID\n");
printf("4 -> Search a student record by Surname\n");
printf("5 -> Search a student record by Mark\n");
printf("6 -> Quit Student Database");
printf("\n\n");
printf("Enter your choice : ");

scanf("%d",&choice);
switch(choice)
{
case 1:
storeRecord();
break;
case 2:
searchStudentByName();
break;
case 3:
searchStudentById();
break;
case 4:
searchStudentByLastname();
break;
case 5:
searchStudentByMark();
break;
case 6:
goto lend;
}
}
lend:;
return 0;
}

提示:将包含错误消息的 default 子句添加到 switch(choice) 并将 if (choice != 5) 更改为更好的测试。

关于c - 姓名和姓氏搜索功能将无法使用。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27734832/

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