2016-04-04 6 views
-4

Ich bin auf eine Situation mit meinem Buch Borrow-Funktion stecken, Das Problem ist, wenn ich Benutzer eingegeben Lehrer-ID mit einer vorhandenen ID in der Teacherfile vergleichen muss. meine ID-Datentypen für beide sind integer so i verwendet:If (int IDnumvariable == structname [Zähler] .IDnumber) funktioniert nicht

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 

aber nicht richtig zu vergleichen oder die Speicherung der ID i aus der Datei gelesen, und die Datei vorhanden ist und das hat die gespeicherte ID für Tests

angebracht ist der Code-Block von meinem Lehrer borgen Funktion jede Hilfe

i zur Herstellung der ID-Datentypen char dachte würde geschätzt und verwenden strcmp (userentered, struct [count] .idnum)

+0

Warum machst du das: tid2 = tid2 + teach [i] .IDnum; ? Wo machst du den Vergleich (du erwähntest im Titel)? –

+0

das war beabsichtigt, um die ID-Variable zu speichern, die ich aus der Datei gelesen habe, um mit der Benutzer eingegebenen ID –

+3

zu viel Code zu vergleichen. Präsentiere deine [mcve]. Wählen Sie auch _eine_ Sprache. Nicht zwei. –

Antwort

0

(schreiben Sie die Kommentare) Entfernen Sie tid2= tid2 + teach[i].IDnum;.

Ja, können Sie if (tid == teach[i].IDnum)

jedoch verwenden, sehen Sie, was Ihre while(1) Schleife in tborrow tut:

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. 

Überlegen Sie, wie Sie dies tun würden, wenn Sie für einen Lehrer von Hand gesucht haben in einer Liste auf Papier - würdest du den ersten Lehrer in der Liste sehen, dann aufgeben und wieder anfangen, wenn es nicht der richtige wäre?

+0

Ich bekomme, was Sie sagen, ich entfernte diesen Abschnitt des Codes. Die while (1) loop meine Absicht war es zu laufen, bis es das Ende der Datei erreicht –

+0

wäre es am besten, nur eine for-Schleife mit einem festgelegten Betrag zu verwenden, ich versuche, die while (1) -Schleife so umzuschreiben es inkrementiert und dann wieder loops, sollte ich einen statischen Zähler haben? –

+0

Sie sollten einfach in der while-Schleife weitermachen, bis Sie den Lehrer gefunden haben, nach dem Sie suchen, oder drücken Sie eof. Sobald Sie eof drücken, sollten Sie die Fehlermeldung anzeigen. –

Verwandte Themen