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.