学校作业问题。一切都在编译,但没有任何回报......我错过了什么?

时间:2017-04-02 18:36:00

标签: c++

我已经写出了学校作业的代码,但它并没有正常工作。一切都在编译和运行,但我遇到了几个问题。前几个菜单选项都没有在屏幕上返回任何内容。当我从文件中加载对象数组时,它几乎让我犯了错误。

过去几个小时我一直在看这个,而且似乎无法找出我做错了什么,所以我转向有更多经验的人我有。

*编辑删除了一些您可能无需帮助的代码。

以下是似乎给我带来问题的功能。它们在主程序的开关中被调用。

这是main()

int main()
{
    //Arrays
    BookClass books[MAX_BOOKS];

    //Variables
    int choice;     //For menu selection from getMenuChoice.
    int bookNum;    //For holding array location of book for the findBook function.
    int numBooks;   //Holds the number of books in the array
    string searchTitle;     //Hold value of user's search for findBook function.
    BookClass newBook;
    fstream file;   //For input of file to load array

    getBooks(books, numBooks);

    do
    {
        choice = getMenuChoice();

        if (choice != 6)
        {
            switch (choice)
            {
            case 1:
            {
                showBooks(books, numBooks);
                break;
            }
            case 2:
            {
                showTitles(books, numBooks);
                break;
            }
            case 3:
            {
                cout << "\nEnter the title you wish to find: ";
                getline(cin, searchTitle);
                cin.ignore();
                findBook(searchTitle, books, numBooks);
                bookNum = findBook(searchTitle, books, numBooks);
                break;
            }
            case 4:
            {
                cout << "\nEnter the title you wish to check out: ";
                getline(cin, searchTitle);
                cin.ignore();
                findBook(searchTitle, books, numBooks);
                bookNum = findBook(searchTitle, books, numBooks);
                books[bookNum].returnBook();
                break;
            }
            case 5:
            {
                cout << "\nEnter the title you wish to return: ";
                getline(cin, searchTitle);
                cin.ignore();
                findBook(searchTitle, books, numBooks);
                bookNum = findBook(searchTitle, books, numBooks);
                books[bookNum].checkOutBook();
                break;
            }
            }
        }
        if (choice == 6)
        {
            cout << "\nThank you for using the library program!! This program will now end!" << endl;
        }
    } while (choice != 6);

    system("pause");
    return(0);
}

以下这些功能似乎给了我一些问题......

//Function definition for getBooks. Loads the 'books' array using data from input file 'books.txt'
void getBooks(BookClass books[], int &numBooks)
{
    string bookTitle, authorName, bookPublisher, bookISBN;
    double bookPrice;
    int bookYear, bookInStock;
    int total = 0;
    ifstream file;
    file.open("books.txt", ios::in);


    if (file.fail())
    {
        cout << "\nThe file failed to open. Please restart the program and try again!" << endl;
        system("pause");
        return;
    }

    for (int i = 0; i < numBooks; i++)
    {
        getline(file, bookTitle);
        getline(file, authorName);
        getline(file, bookPublisher);
        getline(file, bookISBN);
        file >> bookPrice;
        file >> bookYear;
        file >> bookInStock;

        books[i].storeBook(bookTitle, authorName, bookPublisher, bookISBN, bookPrice, bookYear, bookInStock);

        books[i].displayBookInfo();
        numBooks++;
    }
    file.close();

}



//Function definition for showBooks. Loops through the 'books' array and displays info for all books to user.
void showBooks(BookClass books[], int numBooks)
{
    for (int i = 0; i < numBooks; i++)
    {
        books[i].displayBookInfo();
        cout << endl;
    }

}



//Function definition for showTitles. Similar to showBooks. Only displays the titles of the books to the user.
void showTitles(BookClass books[], int numBooks)
{
    for (int i = 0; i < numBooks; i++)
    {
        books[i].getTitle();
        cout << endl;
    }

}

感谢任何帮助。对不起,如果任何格式或任何性质的东西都没有完全正确完成。我在这里很新。因此,对未来的任何批评也受到欢迎和赞赏!

0 个答案:

没有答案