gpt4 book ai didi

c - If(int IDnumvariable == structname[counter].IDnumber) 不起作用

转载 作者:行者123 更新时间:2023-11-30 21:28:56 27 4
gpt4 key购买 nike

我陷入了图书借阅功能的困境,问题是当我必须将用户输入的教师 ID 与教师文件中的现有 ID 进行比较时。我的 id 数据类型都是整数,所以我使用:

    void tborrow() //2.1
{
int bksb=0;
int tbid2 =0;
struct books book[50];
struct teachers teach[50];
int x=0;
int c=0;
int i=0;
int tid=0;
int tid2=0;
int tbid=0;
FILE *teacherp;
FILE *bkbp;


teacherp= fopen("TeacherFile.txt", "r+");

if(teacherp!=NULL) //Checks if Teacher File was created successfully
{

printf("Teacher File Successfully Opened\n\n\n");
printf("Enter Teacher's ID# :");
scanf("%d",&tid);


while(1) // Loop till end of file
{
fflush(stdin);////flushes buffer
fscanf(teacherp,"%s %d %s %s %d ",teach[i].Teachname,&teach[i].IDnum,teach[i].contactnum,teach[i].Faculty,&teach[i].bksborrowed);
tid2= tid2 + teach[i].IDnum;
if (tid == tid2)
{
system("cls");
printf("Teacher ID Confirmed\n\n");

printf("\n\n----Teacher Information-----"); //check if correctly entered
printf("\nName: %s ", teach[i].Teachname);
printf("\nID #: %d " ,teach[i].IDnum);
printf("\nContact #: %s ", teach[i].contactnum);
printf("\nFaculty: %s " ,teach[i].Faculty);
printf("\nBooks Borrowed: %d " ,teach[i].bksborrowed);
printf("\n-----------------------------\n");
printf("\nIs your account? 1-Yes 0=No : ");
scanf("%d",&c);
fflush(stdin);//flushes buffer after scanf

if (c==1)//if yes find and borrow book
{
bksb = bksb+teach[i].bksborrowed;
if (bksb < 2)// if teacher has less than 2 books borrowed
{
printf("\nThis account currently has %d books borrowed",teach[i].bksborrowed);

bkbp = fopen("BookFile.txt","r+");

if(bkbp!=NULL) //Checks if Book File was created successfully if yes search for book and borrow
{

printf("Enter Book ISBN # :");
scanf("%d",&tbid);
while(1)
{

fscanf(bkbp,"%s %s %d %d %s %d %d-%d-%d %d-%d-%d ",book[x].Title, book[x].Author, &book[x].Accessionnum, &book[x].ISBN, book[x].Available, &book[x].bid, &book[x].bdate->m, &book[x].bdate->d, &book[x].bdate->y, &book[x].rdate->m, &book[x].rdate->d, &book[x].rdate->y);
tbid2 = tbid2 + book[x].ISBN;
if (tbid == tbid2)
{
printf("\n\n-------Book Information-------"); //check if correct book found
printf("\nTitle: %s", book[x].Title);
printf("\nAuthor: %s" ,book[x].Author);
printf("\nAccession#: %d" ,book[x].Accessionnum);
printf("\nISBN#: %d", book[x].ISBN);
printf("\nAvailability(Y-N): %s" ,book[x].Available);
printf("\nBorrowers ID#: %d ",book[x].bid);
printf("\nBorrow Date: %d-%d-%d ",book[x].bdate->m,book[x].bdate->d,book[x].bdate->y);
printf("\nReturn Date: %d-%d-%d ",book[x].rdate->m,book[x].rdate->d,book[x].rdate->y); //Prints user entry to screen for confirmation
printf("\n------------------------------\n");
printf("\nIs this the book you are looking for? 1-Yes 0=No : ");
scanf("%d",&c);
fflush(stdin);//flushes buffer after scanf
if (c==1)//if yes print into file
{
book[x].bid=tid2;
bksb++;
teach[i].bksborrowed=bksb;
printf("\n Book %s has been borrowed by %s %d",book[x].Title,teach[i].Teachname,teach[i].IDnum);
fclose(bkbp);
printf("\n\nReturning to previous account");
_getch();
break;
TeacherAcc();
}
/*else
{
fclose(bkbp);
system("cls");
printf("\nStarting over book borrowing!");
break;
tborrow();
}*/


}
else
//if we encountered the end of the file on the last attempt
//to read data then break out of the read loop
if( feof(bkbp) ) //If end of file is reached break out of loop
{
break;
}

++x;
}




}else // if book file failed to open
{
printf("Error!, Restarting Teacher Book Borrowing System. Press Enter to continue");
getchar();
tborrow();
}
}else if (teacher[i].bksborrowed == 2) // if teacher has already borrowed 2 books
{
printf("\nThis account already has %d books borrowed",teach[i].bksborrowed);
printf("\nGoing back to previous menu");
_getch();
system("cls");
TeacherAcc();
}

}
else if (c==0) //runs the Teacher borrow function if wrong account
{
system("cls");
printf("\nRe-Enter Teacher ID!");
tborrow();
}




}else // else if teacher id not found
{
printf("\nID %d not found, Please re-enter a valid ID \n Press enter to try again",tid);
_getch();
system("cls");
tborrow();
}
if( feof(teacherp) ) //If end of file is reached break out of loop
{
break;
}else

i++;
}//end of continuous loop

fclose(teacherp); //close the file when done
} //if file created successfully
else //Teacher file failed to load, restarts function to correctly open Teacher
{
printf("Error!, Restarting Teacher System. Press Enter to continue");
getchar();
system("cls");
}
printf("\n\nReturning to previous menu, Press Enter to continue \n");
fflush(stdin);//flushes buffer so getchar works properly
getchar();
system("cls");
TeacherAcc();
}//borrow function end

但它没有正确比较或存储我从文件中读取的 id,并且该文件存在并且具有用于测试的存储 id

附件是我老师借用函数的代码块任何帮助将不胜感激

我正在考虑将 id 数据类型设为 char 并使用 strcmp(userentered,struct[count].idnum)

最佳答案

(形成评论)删除 tid2= tid2 +each[i].IDnum;

是的,您可以使用if (tid ==each[i].IDnum)

但是,看看 tborrow 中的 while(1) 循环正在做什么:

while(1)
read a teacher from the file
is the teacher the right one?
yes - do something
no - print an error and recursively call tborrow again.

想一想,如果您在纸上的列表中手动寻找老师,您会如何做 - 您会查看列表中的第一位老师,然后放弃并重新开始(如果不是合适的老师) ?

关于c - If(int IDnumvariable == structname[counter].IDnumber) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36392854/

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