首页 > 解决方案 > 缺少数组元素问题

问题描述

这是我最近完成的家庭作业的一部分。我需要使用一系列结构来按标题和作者存储图书库。根据用户输入按字母顺序对作者或标题进行排序和显示时,代码运行良好。

我遇到的唯一问题是它显示所有书籍按字母顺序排序。与作业一起使用的文本文件中共有 14 本书,但当输入全部显示 (S) 选项时,仅显示 13 本书。

错误的一个例子是:

()

Audio for Games (Brandon)

代替:

Audio for Games (Brandon)
Beginning LINUX Programming (Stones and Matthew)

我的代码:


#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

// structure
struct Book {
    string title;
    string author;
};

const int ARRAY_SIZE = 1000;
Book books[ARRAY_SIZE];
string pathName;
ifstream lib;

// global variables
int loadData();
void showAll(int count);
void sortByTitle(int count, string title);


int main()
{
    // initialised variables
    int count = 0;
    char selector = 'q', yesNoAnswer = 'n';
    string name;
    string title;

    // asks user for file pathname
    cout << "Welcome to Tommy's Library Database." << endl;
    cout << "Please enter the name of the file: ";
    getline(cin, pathName);
    loadData();
    count = loadData();
    cout << count << " Records loaded successfully." << endl;
    // Switch case menu
    do {
        cout << endl << "Please enter a keyword that corresponds to the list of options: " << endl;
        cout << " Search by: (A)uthor, (T)itle, (S)how All, (Q)uit Program: ";
        cin >> selector;
        selector = toupper(selector);
        switch (selector)
        {
        case 'S':
            sortByTitle(count, title);
            if (count <= 0) {
                cout << " No counts found! " << endl;
            }
            else {
                showAll(count);
            }
            break;
        }
    }
    while (selector != 'q' && selector != 'Q');
    return 0;
}

int loadData()
{
    int count = 0;
    int i = 0;

    lib.open(pathName);
    ifstream lib(pathName);

    if (!lib)
    {
        cout << " Unable to open file path! " << endl;
        return -1;
    }
    while (!lib.eof())
    {
        getline(lib, books[count].title);
        getline(lib, books[count].author);
        count++;
    }
    return count;
}
// displays all book titles beside the author names
void showAll(int count)
{
    for (int i = 0; i < count; i++)
    {
        cout << books[i].title << " " << "(" << books[i].author << ")" << endl;
    }
}
// Sorts by book title.
void sortByTitle(int count, string title) {
    Book temp;
    for (int i = 0; i < count; i++) {
        for (int j = 0; j < count - i; j++) {
            if (books[j].title > books[j + 1].title) {
                temp = books[j];
                books[j] = books[j + 1];
                books[j + 1] = temp;
            }
        }
    }
}

我与作业一起使用的文本文件(books.txt)

Objects First with Java
Barnes and Kolling
Game Development Essentials
Novak
The Game Maker's Apprentice
Overmars
C++ Programming: From Problem Analysis...
Malik
C++ Programming Lab Manual
Scholl
Beginning LINUX Programming
Stones and Matthew
C++ Programming: Program Design Including...
D. S. Malik
C++ How to Program
Deitel and Deitel
Programming and Problem Solving with C++
Dale, Weems, Headington
Game Character Development with Maya
Ward
Developing Games in Java
Brackeen
C# Programming
Harvey, Robinson, Templeman, Watson
Java Programming
Farrell
Audio for Games
Brandon

标签: c++arrayssorting

解决方案


当您的书籍数组从 1 开始时,您在方法中从 0showAll()开始循环,只需从 1 开始循环并转到 count + 1

for (int i = 1; i < count + 1; i++)

推荐阅读