이 코드의 목적은 기본적인 도서관 체크인 체크 아웃 시스템이며, 스캐너를 사용하여 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;
}
라이브러리 데이터베이스를 열고 운영자 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 );
또한 프로그램을 별도의 함수로 나누고 ( "팩터링") 각 함수를 개별적으로 테스트하십시오.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다