First off this is a homework question, please don't do my homework for me as nice as that would be...

I have to make a program that can search a library for an author or title from a file and output all the matching lines. I am very close but right now the only thing my program works for is the very last author/title line. Here is my code:

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


using namespace std;

const int SIZE = 1000;
string bookTitle[SIZE];
string bookAuthor[SIZE];
string filePath;

int loadData(string);
int showAll(string);
int authorSearch(string, string);


int count = 0;

int main(){

// Initialize variables
string userPath;
string searchA;
char userChoice;

// Get the file from the user
cout << "Welcome to the super duper programming library" << endl;
cout << "Enter in a file path: ";
getline(cin, userPath);
cout << endl;

// Call the loadData function
loadData(userPath);

// Create the menu
cout << "How would you like to search the library?" << endl
<< "Q to quit" << endl
<< "S to show all" << endl
<< "A to search by author" << endl
<< "T to search by title" << endl;
cout << "Choice: ";
cin >> userChoice;

// Switch to based on userChoice

switch (userChoice) {
case 'Q':
break;
case 'S':
cout << endl << "There are " << showAll(userPath) << " books in the library";
break;
case 'A':
cout << "Enter in the the author name: ";
cin >> searchA;
cout << endl << authorSearch(searchA, userPath);

}

cout << "Thanks for using Joe's library service";





// Pause and exit
    getchar();
    getchar();
    return 0;
    }

    // loadData opens the specified file, and checks to make sure it is open
    int loadData(string passedPath){
    // Initialize neccesary ifstream variables
    string line;
    ifstream filePath;
    // Open the file
    filePath.open(passedPath.c_str());
    // Return -1 if the file was entered wrong
    if(!filePath.is_open()){
    return -1;
    }

    while (!filePath.eof()){
    getline(filePath,line);
    bookTitle[SIZE] = line;
    getline(filePath,line);
    bookAuthor[SIZE] = line;
    count++;
    }

    // Return 0
    return 0;
    }

    int showAll(string passedPath2){
    // Initialize neccesary variables
    string line;
    ifstream filePath;

    filePath.open(passedPath2.c_str());

    // Put book title and book author into appropriate arrays and display them
    while (!filePath.eof()){

    getline(filePath,line);
    bookTitle[SIZE] = line;
    getline(filePath,line);
    bookAuthor[SIZE] = line;

    cout << bookTitle[SIZE] << " (" << bookAuthor[SIZE] << ")" << endl;
    count++;
    }

    // Return the number of books
    return count / 2;
    }

    int authorSearch(string aSearch, string passedPath3){
    ifstream filePath;
    int j;
    for(j = 0; j < count; j++){

    if(bookAuthor[SIZE] == aSearch){
    cout << bookTitle[SIZE] << " (" << bookAuthor[SIZE] << ")" << endl;
    return j;
    } else {
    cout<< "not found";
    }
    }

    return j + 1;
    }

I know it's a lot, but could some one point out my error and give me a push in the right direction? Thanks.

lines 86 and 89 are the problem. You need to use a counter that counts the number of times getline() is called so that you can put the data in the appropriate item of the arrzy. Start the counter at 0 and increment it at the end of that read loop.

Ditto for lines 108 and 110

I see you already have a variable named count -- just use that, don't forget to reset it back to 0 before each of those two loops.

while (!filePath.eof()){
        int i = 0;
        int j = 0;

        for(i = 0; i < count; i++){
        getline(filePath,line);
        bookTitle[SIZE] = line;
        }

        for(j = 0; j < count; j++){
        getline(filePath,line);
        bookAuthor[SIZE] = line;
        }
        count++;

    }

So based on your recommendation I tried this, but it must have not been what you meant... I also tried a do while that incremented i++. What else should I try?

I meant something like this. Next question is how is the file formatted? Post a couple examples of the lines.

    count = 0;
    while(getline(filePath,line)) {
        bookTitle[count] = line;
        count++;
    }

its formated like:
book author
book title
book author
book title

etc.

They need to be read into parallel arrays.

bookTitle[SIZE]

Every time you specify bookTitle[SIZE] you are specifying the memory just past your bookTitle array. Haven't you been taught about array indecies yet?

while (!filePath.eof()){ -- See this

for(i = 0; i < count; i++){ -- Do you actually know the value of count?

count should be the number of times it goes through my !filePath.eof loop, which is the number of books if I getline twice within that loop.

I understand that to access an array you need some kind of loop to go through it for instance:

for (i = 0; i < 5; i++)
    cout << array[i]

Should display 5 values of that array, but when I try to do this in my search function it doesn't come up with anything, not even the last book and author combo.

I'm pretty stumped honestly.

Here's what I'd do:

Start over.
Step 1) Read that link I posted.

Step 2) Read the data into the arrays. As soon as you put a line into the array, write the array entry to the screen. Think about the loop and when it should end. Refer to Step 1.

When done reading:

Step 3) In a loop display the entire contents of the arrays. Make sure things were read in and stored properly.

When all that works completely, continue to the next step.

It will also help greatly if you format your code better.

I fixed it! I used basically my original code but instead of going getline and then cin array[SIZE], I did array[count], then I got rid of all my [SIZE]'s and switched them to the variables being counted.

I will go through and read those two links you posted.

Is there anything specifically that would make my code look better?

WaltP and AncientDragon, thank you so much for your time and effort, I really, really appreciate it.

Is there anything specifically that would make my code look better?

yeah, the links.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.