2014-12-18 2 views
0

이 코드의 목적은 기본 라이브러리 체크인 체크 아웃 시스템으로 ISBN 번호 인 바코드를 입력 한 다음 프로그램이 내 .txt 데이터베이스를보고 검색합니다 책 이름 그런 다음 책을 체크 아웃하고 학생 ID 번호를 묻고 ISBN, out/in, id 번호 및 현재 시간을 FILE로 출력 할 것인지 묻습니다. 이 프로그램은 처음에는 잘 작동하지만 바코드를 입력 한 후에 다른 책을 체크 아웃하려고하면 작동을 멈 춥니 다. 그냥 멈추지 않으면 입력 캔트 이동을하지 않습니다.올바르게 루핑되지 않음

#define _CRT_SECURE_NO_WARNINGS 
#include <iostream> 
#include <iomanip> 
#include <fstream> 
#include <string> 
#include <ctime> 
using namespace std; 

int main() 
{ 

char variable = 1; 
ifstream infile; 
ofstream outfile; 
infile.open("Library_Database.txt"); 
outfile.open("Library_Checkout.txt", ios::app); 

do { 
    char variable; 
    cout << "SETSUCON MANGA LIBRARY DATABASE\n\n" << setw(45) << "For Check-Out please enter: O or o\n" << setw(45) << "For Check-In please enter: I or i\n" << setw(45) << "For Assistance please enter: H or h\n" << setw(45) << "To close this program please enter: Q or q\n" << setw(50) << "Enter an option: "; 
    cin >> variable; 
    cout << "\n"; 

    if (variable == 'O' || variable == 'o'){ 

     double search; 
     double ISBN = 0; 
     string bookName; 

     cout << "Please Scan Barcode: "; 
     cin >> search; //runs fine one time then gets stuck here after I input the ISBN number the second time 

     while (ISBN != search){ 
      infile >> ISBN >> bookName; 
     } 
     cout << "You would like to check out: " << bookName << "?" << endl; 

     string idNumber; 
     char yesOrNo; 
     cout << "Yes or No (Y or N): "; 
     cin >> yesOrNo; 
     if (yesOrNo == 'Y' || yesOrNo == 'y'){ 
      cout << "Please input Student ID number or Badge ID Number: "; 
      cin >> idNumber; 
      cout << endl; 
      time_t now = time(0); 
      char* dt = ctime(&now); 

      outfile.setf(ios::fixed); 
      outfile.precision(0); 

      outfile << search << std::resetiosflags(std::ios::showbase) << setw(12) << idNumber << setw(4) << "out" << setw(30) << dt << endl; 
     } 
     else{ 
      cout << endl << "\n"; 
      continue; 
     } 
    } 

    else if (variable == 'I' || variable == 'i'){ 

     double search; 
     double ISBN = 0; 
     string bookName; 

     cout << "Please Scan Barcode: "; 
     cin >> search; //runs fine one time then gets stuck here after I input the ISBN number the second time 

     while (ISBN != search){ 
      infile >> ISBN >> bookName; 
     } 
     cout << "You would like to check in: " << bookName << "?" << endl; 

     string idNumber; 
     char yesOrNo; 
     cout << "Yes or No (Y or N): "; 
     cin >> yesOrNo; 
     if (yesOrNo == 'Y' || yesOrNo == 'y'){ 
      cout << "Please input Student ID number or Badge ID Number: "; 
      cin >> idNumber; 
      cout << endl; 
      time_t now = time(0); 
      char* dt = ctime(&now); 

      outfile.setf(ios::fixed); 
      outfile.precision(0); 

      outfile << search << std::resetiosflags(std::ios::showbase) << setw(12) << idNumber << setw(4) << "in" << setw(30) << dt << endl; 
     } 
     else{ 
      cout << endl << "\n"; 
      continue; 
     } 
    } 
    else if (variable == 'H' || variable == 'h'){ 
     cout << "This program was written Shea Transue.\n" << "For Assistance Please Contact Shea Transue at 484-264-5863\n\n"; 
    } 
    else if (variable == 'Q' || variable == 'q') { 
     cout << "Thank you for using the SETSUCON LIBRARY DATABASE.\n" << "For assistance please Call or Text Shea Transue at 484-264-5864\n\n"; 
     break; 
    } 
    else { 
     cout << variable << " is not a valid option. Try again\n\n"; 
    } 

} while (variable != 'Q' || variable != 'q'); 

infile.close(); 
outfile.close(); 
return 0; 
} 
+0

디버거에서 실행하고 줄 단위로 코드를 단계별로 실행 했습니까? 그렇지 않은 경우, 지금 어떻게하는지 배울 수있는 좋은 시간입니다. – Cornstalks

+0

전화 번호 목록이 일치하지 않습니다. – Potatoswatter

+0

Visual Studio에서 프로그램을 실행하고 디버거를 사용하는 방법이나 줄 단위로 이동하는 방법을 모르겠습니다. 나는 그것이 내가 가지고 있다고 말하는 오류를 바로 잡는다. 좋은 튜토리얼을 제안 해 주시겠습니까? 또한 나는 내 전화 번호 롤 오타를 만들었습니다 감사합니다. – Shea

답변

0

당신은 라이브러리 데이터베이스 infile를 열고 >> 연산자 그것을 통해 스캔,하지만 당신은 infile.seekg(0, std::ios::beg)으로 다시 처음으로 추구하지 않습니다.

모든 서적을 찾을 수 없으면 while 루프가 종료되지 않습니다. 중간에있는 데이터베이스 만 스캔하기 때문에 첫 번째 이후의 모든 책을 찾을 수 없습니다.

이것을 시도 :

또한
do { 
    cout << "Please Scan Barcode: "; 
    cin >> search; //runs fine one time then gets stuck here after I input the ISBN number the second time 

    infile.clear(); 
    infile.seekg(0, std::ios::beg); 
    while (infile && ISBN != search){ 
     infile >> ISBN >> bookName; 
    } 
    if (! infile) { 
     std::cout << "The book was not found in the database.\n"; 
    } 
} while (! infile); 

("인수 분해") 별도의 함수로 프로그램을 분할하려고 별도로 각각의 기능을 테스트한다.

+0

고맙습니다. 바로 해결했습니다! 나는 방금 대학에서 처음으로 공학 프로그래밍 수업을 마쳤고, 나는 여전히 이것에 매달려서 모든 다른 기능을 배우려고 노력하고있다! 다시 한 번 정말 감사드립니다. – Shea

관련 문제