Searching an Array

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.
first of all, this is not an issue, but where you wrote //initializing variables that is wrong. ur declaring variables. initializing is giving a variable an initial value for example int aramilsNumber = 10;. second since load data returns an int, you have to have an int variable equal the calculation of the function. i.e. aramilsNumber = loadData(userPath);. same with show all
Hahaha showing my noobness. I'm confused about your second point though could you clarify a bit? Thank you so much for your time.
so, whenever you give a function a return value, something has to hold that value. to make that happen you need to declare a variable with the same type as the return value and have it equal the function. if you don't want it to return a value then make it type void
Topic archived. No new replies allowed.