没有正确循环

时间:2014-12-18 05:55:12

标签: c++ loops

此代码的目的是作为基本的图书馆签到结账系统,我使用扫描仪输入条形码,这是ISBN号,然后程序查看我的.txt数据库并搜索书名。然后它会询问您是否要查看该书并询问学生证号码,然后将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;
}

1 个答案:

答案 0 :(得分: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 );

另外,尝试将程序划分为(&#34; factoring&#34;)程序,分别测试每个函数。