0

Hi I am pretty new to c++ programming so just bear with me. I have a library txt file that has 14 book titles along with their authors. I am trying to load this library into an array then accept user input in order to search the library either by title or by name of author. This is my code so far...

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

const int ARRAY_SIZE = 1000;
string bookTitle [ARRAY_SIZE];
string bookAuthor [ARRAY_SIZE];

int loadData(string pathname);
void showAll(int count);
int showBooksByAuthor (int count, string name);
int showBooksByTitle (int count, string title);


int main ()
{
    int numFound, nextNumFound, number, numOfBooks;
    char reply;
    string bookTitles[5], authorName[5], backupFile;
    cout << "Welcome to Brigham's library database." << endl;
    cout << "Please enter the name of the backup file:";
    cin >> backupFile;
    numOfBooks = loadData (backupFile);
    if (numOfBooks == -1) {
        cout << endl;
    } else {
        cout << numOfBooks << " books loaded successfully." << endl;
    }


    do {
        cout << "Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All: ";
        cin >> reply;
        switch (reply) {
            case 'a':
            case 'A':
                cout << "Author's name: ";
                cin.getline(authorName, 5);
                numFound = showBooksByAuthor(numOfBooks, authorName);
                cout << numFound << " records found.";
                cout << endl;
                break;
            case 'q':
            case 'Q':
                cout << endl;
                break;
            case 's':
            case 'S':
                showAll(numOfBooks);
                cout << endl;
                break;
            case 't':
            case 'T':
                cout << "Book title: ";
                getline(cin, bookTitles);
                numFound = showBooksByTitle(numOfBooks, bookTitles);
                cout << nextNumFound << " records found.";
                cout << endl;
                break;
            default:
                cout << "Invalid input" << endl;
                break;          
        }
    } while (reply != 'q' && reply != 'Q');
}


int loadData (string pathname){
    int count = 0, noCount = -1;
    ifstream inputFile;
    string line, reply;

    inputFile.open(pathname.c_str()); 

    if (!inputFile.is_open()) { //If the file does not open then print error message
        cout << "Unable to open input file." << endl;
        return noCount;
    }

        for (int i = 1; i < ARRAY_SIZE; i++) {
            getline(inputFile, bookTitle[i]);
            getline(inputFile, bookAuthor[i]);
            //cout << bookTitle[i] << endl;
            //cout << bookAuthor[i] << endl;    
            count++;
             if(inputFile.eof()) {
                return count;
             }
        }
}

void showAll (int count) {
    for (int j = 1; j <= count; j++) {
        cout << bookTitle[j] << endl;
        cout << bookAuthor[j] << endl;
    }
}

int showBooksByAuthor (int count, string name) {
    int numByAuthor;
    for (int k = 1; k <= count; k++) {
        if(name == bookAuthor[k]) {
            cout << bookTitle[k] << " (" << bookAuthor[k] << ")" << endl;
            numByAuthor++;
        }
    }
    return numByAuthor;
}

int showBooksByTitle (int count, string title) {
    int numByTitle;
    for (int i = 1; i <= count; i++) {
        if (title == bookTitle[i]) {
            cout << bookTitle[i] << " (" << bookAuthor[i] << ")" << endl;
            numByTitle++;
        }
    }
    return numByTitle;
}

So my problem is my int showBooksByAuthor and int showBooksByTitle functions. Currently when the user inputs 't' or 'a' to look up book by title or by author it doesn't even prompt them to enter a title or author it just automatically spits out 0 records found. This error started occurring after I changed the case 't' and case 'a' in the switch statement to getline(cin, stringname) from cin >> stringname. I realized that if they were to enter any input larger than 1 word then the function would fail because I know that cin reads up to the first whitespace. But I don't understand why the getline function isn't working. Any ideas from my code? Sorry if it is badly formatted.

Dietmar Kühl
  • 141,209
  • 12
  • 196
  • 356
B T Gingy
  • 15
  • 6
  • 1
    Try reducing your example to the minimum code that exhibits the problem. You will get faster and more useful replies if your code is easy to check, and more importantly, you'll probably solve your own problem when you strip the code down some more. – PeteB Dec 01 '15 at 02:37
  • Sorry for the duplicate. Thanks for the pointer I'll be sure to keep my posted code limited to the problem. – B T Gingy Dec 02 '15 at 20:37

0 Answers0